Is it possible in django to have a ChoiceField on a formset level rather than an inline form? So for example if I have a formset for phones and each inline form represents a single Phone model, how can I have a ChoiceField that spans all the inline forms? Something like this where I’m choosing a primary phone:

My models:
class Profile(models.Model):
verified = models.BooleanField(default=False)
primary_phone = models.OneToOneField('Phone', related_name='is_primary', null=True, blank=True)
class Phone(models.Model):
profile = models.ForeignKey(Profile, editable=False)
type = models.CharField(choices=PHONE_TYPES, max_length=16)
number = models.CharField(max_length=32)
@property
def is_primary(self):
return profile.primary_phone == self
I can always remove primary_phone and use a BooleanField in Phone to indicate if it’s primary or not, but I’m not sure if this going to help my problem.
I’m also looking for a less-hacky more-django-like approach if possible.
There is no way to have django create this for you automatically. In your ModelForm (that’s used in the inline) I’d add a boolean field called is_primary. This field will then show up on each inlined Phone instance (as a checkbox).
On the front end sort it out with javascript so that a user can only select one default at a time. On the back end use some custom validation to double check that only one is_default was submitted, and then update the primary_phone as necessary with form logic.