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
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."
>> 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.