0

Trying to sort a object inside another object. I want to sort by A-name, that is showned in a dropdown. Anyone that a have a idea?

Class A(models.model):
    name = models.CharField(max_length=200)

Class B(models.model):
    value = models.CharField(max_length=200)
    value2 = models.CharField(max_length=200)
    a = models.ForeignKey(A)

Class BForm(Forms.ModelForm):
    class Meta: 
        def myPage()
            myVar = BForm()
Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55
kingRauk
  • 1,259
  • 1
  • 11
  • 24
  • Possible duplicate of: http://stackoverflow.com/questions/923799/reorder-users-in-django-auth and http://stackoverflow.com/questions/2997168/how-do-i-specify-an-order-of-values-in-drop-down-list-in-a-django-modelform – Aya Apr 19 '13 at 13:04

2 Answers2

0

I believe it is just as simple as this:

aObjects = A.objects.order_by('name')[:30]
Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
0

You can set ordering attribute of Meta class of A.

So you can define A as

Class A(models.model) :
    name = models.charfield(max.length=200);

    class Meta:
         ordering = ['name']
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • Perfect! I had look at that solution, but I thougt it would sort a list not a single object. Thanks again. – kingRauk Apr 19 '13 at 13:44