On Dec 30, 7:15 am, janedenone <janeden...@googlemail.com> wrote:
> Hi,
>
> this must be really simple, but all the references to ordering fields
> did not explain how to do this: How can I change the order of entries
> in foreign key lists in the admin change view?
>
> Kind regards,
> Jan

Django will just use the default ordering for the related model. You
can change it for all uses of that model by using the 'ordering'
option in the model's inner Meta class.

If you need to change the order just for a foreign key list, while
leaving the model's default order alone, you can do it by defining a
custom form for the admin change view, override the definition of the
foreign key field on that form, passing it a queryset ordered the way
you want.

For example:

class MyCustomForm(forms.ModelForm):
    my_foreign_key = forms.ModelChoiceField
(queryset=MyRelatedModel.objects.order_by('my_order'))

class MyCustomAdmin(admin.ModelAdmin):
    form = MyCustomForm

admin.site.register(MyModel, MyCustomAdmin)

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