alphabetize options in a ForeignKey dropdown?

15 views
Skip to first unread message

arnold4321

unread,
Oct 1, 2008, 5:54:44 PM10/1/08
to Django users
Hi all,

In the admin interface, if I want the options in a dropdown for a
ForeignKey field to be alphabetized by option rather than ordered by
the value (the numeric ID), how would I do that?

For example, I want something like this...

<select name="xxx" id="xxx">
<option value="" selected="selected">---------</option>
<option value="2">Apples</option>
<option value="1">Bananas</option>
<option value="4">Grapes</option>
<option value="3">Oranges</option>
</select>

...instead of this...

<select name="xxx" id="xxx">
<option value="" selected="selected">---------</option>
<option value="1">Bananas</option>
<option value="2">Apples</option>
<option value="3">Oranges</option>
<option value="4">Grapes</option>
</select>

Thanks,
Ryan

Rock

unread,
Oct 1, 2008, 7:03:27 PM10/1/08
to Django users

Have you tried setting the order within your model? I think that is
the only simple way to tackle this at this time.

class Meta:
ordering = ("fruit",)

Jarek Zgoda

unread,
Oct 2, 2008, 3:00:20 AM10/2/08
to django...@googlegroups.com
Wiadomość napisana w dniu 2008-10-02, o godz. 01:03, przez Rock:

This will change default ordering across the whole site. To change
ordering only in admin app just define ordering in admin class for the
model.

Anyway, I seem to be unable to change ordering of FKs relating to User
object. Any hint?

--
We read Knuth so you don't have to. - Tim Peters

Jarek Zgoda, R&D, Redefine
jarek...@redefine.pl

James Bennett

unread,
Oct 2, 2008, 3:11:57 AM10/2/08
to django...@googlegroups.com
2008/10/2 Jarek Zgoda <jarek...@redefine.pl>:

> Anyway, I seem to be unable to change ordering of FKs relating to User
> object. Any hint?

So, suppose you have the following model, a simple user profile:

from django.contrib.auth.models import User
from django.db import models

class UserProfile(models.Model):
likes_spam = models.BooleanField
twitter_username = models.CharField(max_length=255)
website = models.URLField()
user = models.ForeignKey(User)

And suppose you want the list of users, in the admin, to be
alphabetized by username. In your admin.py file, you'd do:

from django.contrib import admin
from django.contrib.auth.models import User
from django import forms
from yourapp.models import UserProfile


class UserProfileForm(forms.ModelForm):
user = forms.ModelChoiceField(queryset=User.objects.order_by('username'))

class Meta:
model = UserProfile


class UserProfileAdmin(admin.ModelAdmin):
form = UserProfileForm

admin.site.register(UserProfile, UserProfileAdmin)

There are other ways to come at this if you just want to override a
single field, but I'm a fan of just doing the form.


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Jarek Zgoda

unread,
Oct 2, 2008, 3:24:19 AM10/2/08
to django...@googlegroups.com
Wiadomość napisana w dniu 2008-10-02, o godz. 09:11, przez James
Bennett:

>> Anyway, I seem to be unable to change ordering of FKs relating to
>> User
>> object. Any hint?
>

> class UserProfileForm(forms.ModelForm):
> user =
> forms.ModelChoiceField(queryset=User.objects.order_by('username'))
>
> class Meta:
> model = UserProfile
>
>
> class UserProfileAdmin(admin.ModelAdmin):
> form = UserProfileForm
>
> admin.site.register(UserProfile, UserProfileAdmin)
>
> There are other ways to come at this if you just want to override a
> single field, but I'm a fan of just doing the form.


Ouch, that was obvious. :)

Thank you.

webcomm

unread,
Oct 2, 2008, 10:58:53 AM10/2/08
to Django users
Thanks James. That did the trick.

Rock

unread,
Oct 2, 2008, 12:55:17 PM10/2/08
to Django users


On Oct 2, 2:00 am, Jarek Zgoda <jarek.zg...@redefine.pl> wrote:
> Wiadomość napisana w dniu 2008-10-02, o godz. 01:03, przez Rock:
>
> > Have you tried setting the order within your model? I think that is
> > the only simple way to tackle this at this time.
>
> >    class Meta:
> >        ordering = ("fruit",)
>
> This will change default ordering across the whole site. To change  
> ordering only in admin app just define ordering in admin class for the  
> model.
>

My understanding is that this does not work for foreign keys. See
Ticket #6585.

It is sure nice to see Bennett's handy dandy code pattern. Tres
formidable.

Reply all
Reply to author
Forward
0 new messages