Wednesday, December 7, 2011

Re: custom attributes of model field

I may not be clear on what you are trying to accomplish, but maybe
something like

class Form1(forms.ModelForm):
class Meta:
model=Model

def type_one_fields(self):
return [self.fields['a']] # Or use whatever criteria you want
for type_one fields.

def type_two_fields(self):
return [self.fields['b']]

def type_three_fields(self):
return [self.fields['c']]

{% for field in form.type_one_fields %}{{field}}{% endfor %}

I don't think the model is where you'd want to do any form/display
stuff. It would be best kept on the form, or as a template tag. If
you are trying to make a 'generic' configuration such as putting all
text fields in one section, fks in another, etc... you could make a
filter:

{% for field in form|field_type:"text" %}{{field}}{% endif %}
{% for field in form|field_type:"foreignkey" %}{{field}}{% endif %}

The filter would have to be smart enough to inspect the form variable
and pick the right fields either by widget, checking a mapping on the
model, or some other criteria that fits exactly what you are trying to
do.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate