Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 June 13, 2010 04:18:59

d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ordering fields in admin form


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-admin,
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 django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 13, 2010 11:45:33

Alexander J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ordering fields in admin form


You should do it in admin.py

class MyClassAdmin(admin.ModelAdmin):
ordering =

On Sun, Jun 13, 2010 at 6:18 AM, darren <backdoc...@gmail.com> 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-admin,
> 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 django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> 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 django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 13, 2010 13:54:07

b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ordering fields in admin form


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
<jeliucalexa...@gmail.com> wrote:
> You should do it in admin.py
> class MyClassAdmin(admin.ModelAdmin):
>     ordering =
>
> On Sun, Jun 13, 2010 at 6:18 AM, darren <backdoc...@gmail.com> 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-admin,
>> 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 django-us...@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.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@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.
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 13, 2010 15:49:40

Dan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ordering fields in admin form


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_foreignkeyfor 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 =
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
dih0...@gmail.com

On Jun 13, 8:53 am, backdoc <ali...@crotchett.com> 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
>
>
>
> <jeliucalexa...@gmail.com> wrote:
> > You should do it in admin.py
> > class MyClassAdmin(admin.ModelAdmin):
> >     ordering =
>
> > On Sun, Jun 13, 2010 at 6:18 AM, darren <backdoc...@gmail.com> 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 django-us...@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.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django-us...@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.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 June 13, 2010 16:33:24

b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ordering fields in admin form


Ahhh. Beautiful. That's what I was looking for. That information
was on the same page I was reading. But, I didn't know exactly what I
was looking for. Thanks.

For those who may stumble upon this thread, note there was a typo in
the solution provided. The "IF" should have been db_field.name.
Here's the full solution:

30 class AtBatAdmin(admin.ModelAdmin):
31 fields = ('player', 'game', 'result')
32 list_display = ('atbat_id', 'player', 'game', 'result')
33 list_filter = ('player', 'game', 'result')
34 ordering =
35
36 def formfield_for_foreignkey(self, db_field, request, **kwargs):
37 if db_field.name == "game":
38 kwargs =
Score.objects.all().order_by('-scores_id') # replace with your
ordering method
39 return db_field.formfield(**kwargs)
40 return super(AtBatAdmin,
self).formfield_for_foreignkey(db_field, request, **kwargs)

Thanks so much for all of the help.

Darren

On Sun, Jun 13, 2010 at 9:49 AM, Dan Harris <dih0...@gmail.com> wrote:
> 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 =
> 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
> dih0...@gmail.com
>
> On Jun 13, 8:53 am, backdoc <ali...@crotchett.com> 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
>>
>>
>>
>> <jeliucalexa...@gmail.com> wrote:
>> > You should do it in admin.py
>> > class MyClassAdmin(admin.ModelAdmin):
>> >     ordering =
>>
>> > On Sun, Jun 13, 2010 at 6:18 AM, darren <backdoc...@gmail.com> 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 django-us...@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.
>>
>> > --
>> > You received this message because you are subscribed to the Google Groups
>> > "Django users" group.
>> > To post to this group, send email to django-us...@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.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@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.
>
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 15th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support