I have the following models:
class Profile(models.Model): verified = models.BooleanField(default=False) def primary_phone(self): return self.phone_set.get(primary=True) class Phone(models.Model): profile = models.ForeignKey(Profile) type = models.CharField(choices=PHONE_TYPES, max_length=16) number = models.CharField(max_length=32) primary = models.BooleanField(default=False) def save(self, force_insert=False, force_update=False, using=None): if self.primary:
I use Phone in ModelForm as a set of forms. What I'm trying to do is show Phone.primary as a switch next to each instance of Phone . If I do basic as a RadioSelect widget:
class PhoneForm(ModelForm): primary = forms.BooleanField(widget=forms.RadioSelect( choices=((0, 'False'), (1, 'True')) )) class Meta: from accounts.models import Phone model = Phone fields = ('primary', 'type', 'number', )
It will display two reports and they will be grouped together next to each instance. Instead, I'm looking for a way to show only one radio button next to each instance (which should set primary=True for this instance) and have the whole set of radio buttons grouped together so that only one of them can be selected.
I am also looking for a clean way to do this, I can do most of it manually - in my head - but I am wondering if there is a better way to do this, django style way.
Anyone got an idea?
source share