Reputation: 1586
I'd like to order a choicefield on a form in alphabetically order by user
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
busname = models.CharField(max_length=60)
forms.py
self.fields['user'].choices = tuple([(t.user.id, t) for t in UserProfile.objects.all().order_by('user')])
Unfortunately, this isn't working. The order is coming out by user.id
Thanks!
Upvotes: 0
Views: 22
Reputation: 2146
Posting an actual answer from my previous comment:
You will need to set the ordering for the field you want on the user, i.e. instead of
...order_by('user')
try setting the attribute with a double underscore, i.e.
...order_by('user__username')
Upvotes: 1