On 25 Jan., 20:36, Matthias Kestenholz <[email protected]>
wrote:
> Alternatively, you could write your own Field or
> Widget which implements the sorting -- it's not that hard.

I was just starting to write "It doesn't work" when I found this here:
http://www.djangosnippets.org/snippets/1767/

class LazyChoiceField(forms.ChoiceField):
    def __init__(self, *args, **kwargs):
        # remove choices from kwargs
        self._lazy_choices = kwargs.pop('choices',())
        super(LazyChoiceField,self).__init__(*args, **kwargs)

    def __deepcopy__(self, memo):
        result = super(LazyChoiceField,self).__deepcopy__(memo)
        result.choices = self._lazy_choices
        result.choices.sort(lambda x, y: cmp(x[1], y[1]))
        return result

Just needed to add the line with the sorting to the snippet. Well,
actually I don't really understand why it works and when django calls
the __deepcopy__, but supposedly it is called after the translation
takes place, so this is an effective solution. Could be generalized
further (not hardcode the sorting function), but for now it does the
job.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
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