I'm not sure I understood correctly what you wanted to achieve, but if
you want to order the
Persons in the dropdown for the players field by name, you need to
change the order of the queryset
associated with that field.

For a ForeignKey field, a ModelForm generates a ModelChoiceField
http://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield
that has a queryset attribute holding the QuerySet used to populate the dropdow.

You can change the order in the __init__ method of your form:

class RotationForm(forms.ModelForm):
   def __init__(self, *args, **kwargs):
        super(RotationForm, self).__init__(*args, **kwargs)
        self.fields["player"].queryset =
self.fields["player"].queryset.order_by("name")

Or by overriding the field on the form:

class RotationForm(forms.ModelForm):
     player = 
forms.ModelChoiceField(queryset=Person.objects.all().order_by("name"))


Hope this gives you some pointers in the right direction.
Nuno

On Tue, Apr 20, 2010 at 3:09 PM, darren <backdoc...@gmail.com> wrote:
> I am not able to figure out how to order the records that fill the
> drop down list in a model form.  On line 112 below, I have attempted
> to order the Rotation model by player.  But, the drop down list that
> is created for me is not ordered that way.  I also tried relocating
> line 112 to within the Meta class definition.  But, that didn't work
> either.  I see field ordering in the docs.  But, I don't see this
> mentioned.
>
> Any suggestions would be more than appreciated.
>
> 101 class Rotation(models.Model):
> 102     player = models.ForeignKey(Person, to_field='f_name',
> verbose_name='Player', limit_choices_to={'relationship' : 'Player'})
> 103     date = models.DateField('Date', default=datetime.date.today)
> 104     game_type = models.CharField("Game Type",
> choices=(('Scrimmage', 'Scrimmage'), ('Tournament', 'Tournament')),
> max_length=10)
> 105     inning_count = models.IntegerField("Innings Out", default=1)
> 106     def __unicode__(self):
> 107         return '%s || %s %s || %s || %s' % (self.date,
> self.player.f_name, self.player.l_name, self.game_type,
> str(self.inning_count))
> 108     class Meta:
> 109         ordering = ['date']
> 110
> 111 class RotationForm(forms.ModelForm):
> 112     Rotation.objects.order_by('player')
> 113     class Meta:
> 114         model = Rotation
> 115         exclude = ['date', 'inning_count']
>
> --
> 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.

Reply via email to