On Tue, 2007-05-15 at 13:18 +0200, Bram - Smartelectronix wrote:
> Hey everyone,
> 
> 
> How do I change the order of the fields in a newform:
> 
> class ContactForm(forms.Form):
>    to = forms.ModelChoiceField(ContactEmail.objects.all())
>    message = forms.CharField(widget=forms.Textarea())
> 
>    def __init__(self, user, *args, **kwargs):
>      super(ContactForm, self).__init__(*args, **kwargs)
> 
>      # user isn't logged in, ask him for his email!
>      if user.is_anonymous():
>        self.fields['from'] = forms.EmailField()
>      else:
>        self.fields['from'] = forms.EmailField(widget=forms.HiddenInput, 
> initial=user.email)
> 
> Cool, but I would like to see this as:
> 
> From:
> To:
> Message:
> 
> 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.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
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