If you check out the source code in django.forms.forms, the forms use
a SortedDict class that is defined in django.utils.datastructures.

I'd probably go about this by creating a class called something like
SortableForm:

from django.utils.datastructures import SortedDict
class SortableForm(forms.Form):
  def sort_fields(self, fields):
     self.fields = SortedDict([(field, self[field]) for field in
self.fields])

And then just subclass my custom forms off of it. (Disclaimer, I
haven't tested that code yet)

This way, you at least have the same type of "fields" object as with a
regular form.  However, if you're trying to sort the same form
multiple times, you may have some issues.  Also, this is a weird thing
to do, but I won't ask questions…

--
Peter

On Sep 23, 8:09 pm, Scott SA <pydev...@gmail.com> wrote:
> For reasons unimportant to this message, I found it necessary to
> rearrange the order that fields are rendered on some of my forms.
> According to the django docs., I simply needed to rearrange the order
> of fields as I defined them, which works quite fine when I know in
> advance the required order. My use-case does not know in advance (very
> long story)
>
> In models.BaseForm, the display and rendering is managed with a
> dictionary containing the fields.
>
> The rendering method is called "_html_output" and it iterates as
> follows
>     for name, field in self.fields.items():
>
> Because models.BaseForm.fields is a dictionary and dictionaries are
> not inherently sorted, whatever order that dictionary is in, is the
> order the form renders. I've not followed up in the code as to how
> django controls the dictionary order, I just decided to add a bit of
> functionality to solve my requirements (nee problem) dynamically.
>
> Initially I thought of overshadowing "_html_output" with my own, but
> that can be tricky because there is a fair amm't going on in that
> method. It is possible that django may change that method and that
> could spell future trouble.
>
> So here's an outline of roughly how I did it and I'm hoping to hear
> comments as to why/where I may have problems with this:
>
> First, I created a custom dictionary class that knows how to sort its
> items (can do this with values too, just not showing it here):
>
> class sorted_dict(dict):
>     def __init__(self, *args, **kwargs ):
>         super(sorted_dict, self).__init__(*args, **kwargs)
>
>     def items( self ):
>         k = self.keys()
>         k.sort()
>         return [[i, self[i]] for i in k]
>
> Then I was able to do something that looked like this:
>
> class MyFormClass(forms.Form):
>     zebras = forms.CharField()
>     aardvarks = forms.CharField()
>     orangutans = forms.CharField()
>
> my_form = MyFormClass( some_data )
>
> my_form.as_p() returns the original order:
>     <p>zebras...</p>
>     <p>aardvarks...</p>
>     <p>orangutans...</p>
>
> my_form.fields = sorted_dict(my_form.field)
>
> my_form.as_p() returns this order:
>     <p>aardvarks...</p>
>     <p>orangutans...</p>
>     <p>zebras...</p>
>
> Before I overshadowed the dictionary method with my custom behavior,
> the form would render in class definition order, afterwards it renders
> in alphabetical order by fields (in this example).
>
> Can anyone think of a reason that this would break in the future or a
> more django-esque way to accomplish this?
>
> Scott
--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to