Wednesday, October 22, 2014

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

Perhaps the most djangoish way would be to create two model forms, with different fields excluded, and choose which to render in the template.

Or you could render CSS style information to hide one or the other.

Or you could use JavaScript to hide one or the other based on something else that you render.

Or write the template code to render the fields instead of using .as_p, etc.

On Wed, Oct 22, 2014 at 7:48 AM, DJ-Tom <eventellis@gmail.com> wrote:
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.

--
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/CAB%2BAj0vhXTjcKo9Kx6K4tjksWgH%2BpKwgQKsfs6CUnG%3DCNPX_UQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment