samedi 29 novembre 2014

python - Django order_by clause - Stack Overflow


I am new to both django and python but I am beginning to get a grasp on things. I think. I have this problem and I can not seem to find an answer to it. my problem is in django queryset order_by clause


pp=Loksabha.objects.filter(state_name='MAHARASHTRA',constituency_name='Kolhapur').order_by('lok_sabha')

pp result is


Fifteenth Lok Sabha(2009-14)
Eleventh Lok Sabha(1996-98)
Fourteenth Lok Sabha(2004-09)
Tenth Lok Sabha(1991-96)
Thirteenth Lok Sabha(1999-04)
Sixth Lok Sabha(1977-79)
Ninth Lok Sabha(1989-91)
Seventh Lok Sabha(1980-84)
Fifth Lok Sabha(1971-77)
Fourth Lok Sabha(1967-71)
Second Lok Sabha(1957-62)
Twelfth Lok Sabha(1998-99)

but i want to result like this formt


Fifteenth Lok Sabha(2009-14)
Fourteenth Lok Sabha(2004-09)
Thirteenth Lok Sabha(1999-04)
Twelfth Lok Sabha(1998-99)
Eleventh Lok Sabha(1996-98)
Tenth Lok Sabha(1991-96)
Ninth Lok Sabha(1989-91)
Sixth Lok Sabha(1977-79)
Fifth Lok Sabha(1971-77)
Fourth Lok Sabha(1967-71)
Second Lok Sabha(1957-62)

my model is:


class Loksabha(models.Model):
lok_sabha = models.CharField(max_length=255)
member_name = models.CharField(max_length=255)
gender = models.CharField(max_length=10)
constituency_name = models.CharField(max_length=255)
party_name = models.CharField(max_length=255)
state_name = models.CharField(max_length=255)

please give me proper solution.




The result of your queryset should be ordered by the attribute lok_sabbah in alphabetical order from the code you wrote here. Meanwhile, I don't know what you're using when 'printing' your model.


Could you copy/paste us your str method to be sure of the printed result?


Maybe, you just need an order_by(pk) if the names follow the order of their creation...



I am new to both django and python but I am beginning to get a grasp on things. I think. I have this problem and I can not seem to find an answer to it. my problem is in django queryset order_by clause


pp=Loksabha.objects.filter(state_name='MAHARASHTRA',constituency_name='Kolhapur').order_by('lok_sabha')

pp result is


Fifteenth Lok Sabha(2009-14)
Eleventh Lok Sabha(1996-98)
Fourteenth Lok Sabha(2004-09)
Tenth Lok Sabha(1991-96)
Thirteenth Lok Sabha(1999-04)
Sixth Lok Sabha(1977-79)
Ninth Lok Sabha(1989-91)
Seventh Lok Sabha(1980-84)
Fifth Lok Sabha(1971-77)
Fourth Lok Sabha(1967-71)
Second Lok Sabha(1957-62)
Twelfth Lok Sabha(1998-99)

but i want to result like this formt


Fifteenth Lok Sabha(2009-14)
Fourteenth Lok Sabha(2004-09)
Thirteenth Lok Sabha(1999-04)
Twelfth Lok Sabha(1998-99)
Eleventh Lok Sabha(1996-98)
Tenth Lok Sabha(1991-96)
Ninth Lok Sabha(1989-91)
Sixth Lok Sabha(1977-79)
Fifth Lok Sabha(1971-77)
Fourth Lok Sabha(1967-71)
Second Lok Sabha(1957-62)

my model is:


class Loksabha(models.Model):
lok_sabha = models.CharField(max_length=255)
member_name = models.CharField(max_length=255)
gender = models.CharField(max_length=10)
constituency_name = models.CharField(max_length=255)
party_name = models.CharField(max_length=255)
state_name = models.CharField(max_length=255)

please give me proper solution.



The result of your queryset should be ordered by the attribute lok_sabbah in alphabetical order from the code you wrote here. Meanwhile, I don't know what you're using when 'printing' your model.


Could you copy/paste us your str method to be sure of the printed result?


Maybe, you just need an order_by(pk) if the names follow the order of their creation...


0 commentaires:

Enregistrer un commentaire