Malcolm Tredinnick wrote:
>> Is there any way to explicitly order fields?
> 
> This is one of those cases where reading the newforms source is going to
> be the best way to work out the answers. It shouldn't be too hard for
> anybody who wants this kind of depth to trace through what is going on.
> 
> If you have a look in newforms.forms.Form, you can see it uses
> DeclarativeFieldsMetaclass.__new__ to set up the fields (in the
> base_field attribute) and that is a SortedDict. Then BaseForm.__init__
> makes a copy of base_fields for you to modify if you wish and it is
> iterated over a dictionary in other places.
> 
> So you would need to make the fields attribute be a new copy of a
> SortedDict with the fields in the order you want, probably using
> fields.items() and then SortedDictFromList.

Aha! Thanks... I've made a snippet :-)
http://www.djangosnippets.org/snippets/237/


class ContactForm(forms.Form):
     to = forms.ModelChoiceField(ContactEmail.objects.all())
     message = forms.CharField(widget=forms.Textarea(attrs={'rows': 10, 
'cols': 50}))

     def __init__(self, user, *args, **kwargs):
         super(ContactForm, self).__init__(*args, **kwargs)

         # user isn't logged in, so ask him for an email
         from_field = forms.EmailField()

         if not user.is_anonymous():
             from_field.widget = forms.HiddenInput
             from_field.initial = user.email

         # insert the field at the start of the fields
         new_fields = self.fields.items()
         new_fields.insert(0, ('from', from_field))
         self.fields = SortedDictFromList(new_fields)


  - bram

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to