Hi Peter,

On Sep 23, 6:52 pm, Peter Coles <pe...@hunch.com> wrote:
> If you check out the source code in django.forms.forms, the forms use
> a SortedDict class that is defined in django.utils.datastructures.

SortedDict:
    "A dictionary that keeps its keys in the order in which they're
inserted."

VERY sweet indeed!

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

Interestingly, SortedDict also inherits from python's 'dict' class and
overshadows the necessary methods (using zip instead of the iteration
I used) and calls __new__ rather than __init__ of the parent class.

While my code didn't add many lines, this simplifies things indeed. So
much so that no extra code is needed at all! Each time a form field is
added to the dictionary, its order is automatically tracked as the
attribute self.keyOrder within SortedDict.

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

Not the same "form" per se, especially after instantiation, but a
series of dynamically created forms from the same or similar data-
sets. I need to create forms on the fly from an abstracted data
structure but once a form class has been created, it's field order
does not change. If that happens, a new form class is made a la
"interface generator".

I started from these articles from Malcolm Tredinnick and James
Bennett:
    <http://www.pointy-stick.com/blog/2009/01/23/advanced-formset-
usage-django/>
    <http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/>

So with James' method, by adding the fields directly to the class
using:
    self.fields['key'] = forms.<FieldClass>(<field_class_options>)

it maintains and renders in that order. I was making the mistake of
building the dictionary then updating my_form.fields. In other words,
replacing the form's 'SortedDict' with a Python dict object naturally
discards all of the extra functionality, thought most everything else
seems to work fine. SortedDict also allows for the insertion at an
arbitrary index.

Gotta love Python and django!

Thanks Peter (and Malcolm, and James, and... all!)
--~--~---------~--~----~------------~-------~--~----~
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