1

How do I programmatically get the ordering defined in model.Meta for a Django model class or an instance?

i.e. I want to get the ('foo', 'bar') from:

class FooBar(models.Model):
    foo = models.IntegerField()
    bar = models.IntegerField()
    snake = models.CharField(max_length=128)

    class Meta:
        ordering = ('foo', 'bar')

Neither models.FooBar nor models.FooBar.Meta have an attribute ordering

Kimvais
  • 38,306
  • 16
  • 108
  • 142

1 Answers1

1

Are you looking 'private' attribute _meta?

FooBar._meta.ordering

(Also works on instances)

Similar: Get model's fields in Django

Community
  • 1
  • 1
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100