List Info

Thread: ModelForm and fields ordering




ModelForm and fields ordering
user name
2007-12-17 07:13:31
How can I change fields order in form generated using
ModelForm ?

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


Re: ModelForm and fields ordering
country flaguser name
Australia
2007-12-17 07:22:58

On Mon, 2007-12-17 at 05:13 -0800, Vitaliy wrote:
> How can I change fields order in form generated using
ModelForm ?

I think you're approach the problem from the wrong angle.
ModelForm is a
shortcut for converting a model directly to a form. If you
want to alter
a lot of things, such as the order, then you're going to be
writing the
names of all the fields anyway. So write your own little
helper function
to construct a form from your models' fields. Base it on the
code for
ModelForm or something similar, but just write something
specialised.

I'd normally say something like "have a look at how
newforms-admin does
this", but, in this case, that would make things worse
because the Admin
interface needs to be able to handle all kinds of weird
cases, so its
form construction is fairly involved.

Regards,
Malcolm

-- 
Two wrongs are only the beginning. 
http://www.pointy-s
tick.com/blog/


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


Re: ModelForm and fields ordering
user name
2007-12-17 09:02:47
Thanks Malcolm

But I have one little difference between form and model -
only one new
field that needs to be on top of all other fields
example:

#model
class SomeModel(modles.Model):
    field1
    field2
    ...
    field20

#form
class SomeModelForm(forms.ModelForm):
     some_new_field
     class Meta:
          model = SomeModel

thats it !
when form rendered - some_new_field is displayed on the
bottom of all
model's fields, but I need to place it on top...


So i found only one way to solve my problem:
class SomeModelForm(forms.ModelForm):
     def __init__(self, *a, **kw):
          super(...)
          self.base_fields.keyOrder = ['some_new_field',
'field1',
'field2', ...]

I` gues nice, but it would be better if this behaviour will
be in Meta
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )