Wednesday, October 22, 2014

How can I make the field layout of a model form dynamic?

Hi,

I have a model that contains either a text or a numerical information, based on the type of the entry:

equipment = models.ForeignKey(equipment)
number = models.DecimalField('Amount/number', max_digits=6, decimal_places=2, blank=True, null=False, default=0)
text = models.TextField("Text", blank=True, null=True, default='')

Based on that model I created a model form:

class RoomsetupEquipmentForm(ModelForm):
    equipment = forms.ModelChoiceField(queryset=equipment.activeobjects.order_by('category__name', 'name'))

    def __init__(self, *args, **kwargs):
        super(RoomsetupEquipmentForm, self).__init__(*args, **kwargs)
        self.fields['equipment'].choices = equipment_as_choices()

    class Meta:
        model = roomsetup_equipments
        fields = ('equipment', 'number', 'text')

I'm looking for a way to selectively show either the number or the text field when the form is created.

Thomas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/138b45bd-ad73-4d5b-ac7f-27530b8e4e0c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment