I have a simple poll application with models like these:
class Question(models.Model):
text = models.CharField(max_length=250)
position = models.PositiveIntegerField(default=0)
class Variant(models.Model):
question = models.ForeignKey(Question, related_name=u"variants")
text = models.CharField(max_length=250)
position = models.PositiveIntegerField(default=0)
I’d like these variants to be sorted in admin changelist by question first, and then by position so that they wouldn’t mix up with variants from other questions. But simply specifying an ordering in ModelAdmin takes no effect:
class VariantAdmin(admin.ModelAdmin):
list_display = ['text', 'question', 'position']
ordering = ['question', '-position']
The variants with bigger position are listed upper in despite of the ‘question’ ordering.
Is there a way to get this done?
During the writing of this question I tried to find the solution and eventually found it.
The model “Variant” had an ordering specified in Meta class:
This was overriding the ordering set in the model admin. After deleting this all started to work as expected.