I have the following 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)
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
And the following forms:
class PhoneForm(ModelForm):
class Meta:
from accounts.models import Phone
model = Phone
fields = ('type', 'number', )
which is being used in a modelformset_factory.
I’m rendering the formset like this:
<div class="span-13 last">
{{ formset.management_form }}
{% for form in Phones %}
<div class="span-2">{{ form.type|add_class:'dropdown' }}</div>
<div class="span-11 last">{{ form.number|add_class:'phone-number' }}</div>
<div class="clearfix"></div>
{% endfor %}
</div>
Now what I want to do is to render a radio button in the template to reflect the is_primary property of Phone model. There are two ways to determine this relationship, through Phone model itself or through Profile.primary_phone. But then I’m rendering Phone model as a formset, hence looping over its instances, so I tried to include 'is_primary' in PhoneForm fields, but it did not work since it’s a property.
Any idea how to do this?
UPDATE #1:
I have used jpic approach and tried to render primary as radio buttons:
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', )
However, it shows two radio buttons for each instance of Phone while I need it to show only one radio button per instance. I’m going to play around with it for a while and see if I can get it to show correctly.
Instead of:
You should have:
That would make your life easier.
If you cannot make this change: a Phone formset is actually a wrapper around many Phone forms. But the field you’re after allows to edit Profile.primary_phone.
So one way of doing it is to do it manually as such:
But the problem is that the radio won’t have a value for empty Phone forms, as the value is
{{ form.instance.pk }}.Another way of doing it is to add a checkbox to PhoneForm:
We’re using a BooleanField here because for each Phone form, primary is to be set or not. But still, you’ll have to render it yourself:
But then, you need javascript to ensure only one radio is checked at the time, e.g. with jQuery:
Of course, you should update the selectors in this example above to ensure only “primary phone” checkboxes are affected.
Finally, to connect the checkbox, something like this might work: