To change the order of a foreign key in the admin and admin only, you
can override the formfield_for_foreignkey method in your ModelAdmin
for AtBat. See the docs at:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey
for more details, but it sounds like you'll need something like this
(off the top of my head, no idea if it will execute):

def formfield_for_foreignkey(self, db_field, request, **kwargs):
   if db_fieldname=="game":
      kwargs['queryset' ] =
Score.objects.all().order_by('something_to_order_by_here') # replace
with your ordering method
      return db_field.formfield(**kwargs)
   return super(MyModelAdmin, self).formfield_for_foreignkey(db_field,
request, **kwargs)

Hope this helps,

Dan Harris
[email protected]

On Jun 13, 8:53 am, backdoc <[email protected]> wrote:
> This only impacts the order of the records already entered.  But, when
> adding a new record with the admin interface, one of the fields (a
> select box auto generated from line 107 below) is not in the order I
> would prefer.
>
> While typing this email, I just figured out how to order it.  I needed
> to modify the ordering of the model the games column is related to (by
> adding a class META).  While this actually does what I want, the
> downside is that it will impact this model everywhere.  I would still
> like to know if there's a better way do this.
>
> The hard part about googling this or finding it in the Django docs is
> that ordering is a term that applies to so many different aspects.
>
> Thanks.
>
> On Sun, Jun 13, 2010 at 5:45 AM, Alexander Jeliuc
>
>
>
> <[email protected]> wrote:
> > You should do it in admin.py
> > class MyClassAdmin(admin.ModelAdmin):
> >     ordering = ['-myfield']
>
> > On Sun, Jun 13, 2010 at 6:18 AM, darren <[email protected]> wrote:
>
> >> I'm sure the answer is probably documented clearly somewhere.  But, I
> >> can't find it.
>
> >> I would like to change the ordering of a field in a form on the admin
> >> site based on the model below.  The "game" column is several records
> >> long.  I would like to order desc.  I've tried adding a META class to
> >> my model and tinkering with the admin.py.  But, I haven't been able to
> >> figure this out.
>
> >> Could someone point me to the documentation?  I've been reading this:
> >>http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#ref-contrib-a...,
> >> but I don't see what I'm looking for there.
>
> >> Thanks
>
> >> 104 class AtBat(models.Model):
> >> 105     atbat_id = models.AutoField(primary_key=True)
> >> 106     player = models.ForeignKey(Person, to_field='f_name',
> >> verbose_name='Player', limit_choices_to={'relationship' : 'Player'})
> >> 107     game = models.ForeignKey(Score, to_field='scores_id',
> >> verbose_name='Game')
> >> 108     result = models.CharField('Result', choices=(('H', 'Hit'),
> >> ('BB', 'Walk'), ('K', 'Strike Out'), ('ROE', 'Reached On Error'),
> >> ('HBP', 'Hit By Pitch'), ('FO', 'Ground or Fly Out'), ('FC', 'Fielders
> >> Choice'), ('Sacrifice', 'Sac    rafice')), max_length=10)
> >> 109     rbi = models.PositiveSmallIntegerField("RBI", default=0)
> >> 110
> >> 111     def __unicode__(self):
> >> 112         return unicode('%s %s %s %s' % (self.atbat_id,
> >> self.player, self.game, self.result))
>
> >> --
> >> 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.
>
> > --
> > 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.

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