> And when I want to edit 'Something' on the admin site then the
> companies also appear in this order within the select element. Is it
> possible to change this?

Good question! I don't have the One true Answer, but I do have a hack.

WARNING: The following is a vile and despicable hack!

<hack>
Right now, the only way I know of sorting the list is to (gasp) modify
the core Django code. It's a one-line change that sorts the objects
that will be displayed in the drop down.

In db/models/fields/__init__.py, in get_choices, approx. line 349 (in
my version), add the '<<<' indicated line just before the return:
{{{
    def get_choices(self, include_blank=True,
blank_choice=BLANK_CHOICE_DASH):
        "Returns a list of tuples used as SelectField choices for this
field."
        first_choice = include_blank and blank_choice or []
        if self.choices:
            return first_choice + list(self.choices)
        rel_model = self.rel.to
        if hasattr(self.rel, 'get_related_field'):
            lst = [(getattr(x, self.rel.get_related_field().attname),
smart_unicode(x)) for x in
rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
        else:
            lst = [(x._get_pk_val(), smart_unicode(x)) for x in
rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
        lst.sort(lambda x,y: cmp(x[1],y[1])) # <<< This sorts the list
values
        return first_choice + lst
}}}
</hack>

In case this makes someone want to throw things at me, do me (and
others) a favor by explaining The Correct Way to do this. It would be
particularly nice if there is a way to return an arbitrarily filtered/
ordered list for this and the ManyToMany selectors.

I found one discussion at http://code.djangoproject.com/ticket/2445
that is talking about enhancing the limit_choices_to, and it feels
like it will address this also, but after a year it's still a work in
progress.

There's a thread in Django Developers that touches on dynamic
limit_choices.
http://groups.google.com/group/django-developers/browse_thread/thread/7b351d4806b2b85a/bd6785509e75b76f


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