Model Meta inheriting ordering from abstract models

506 views
Skip to first unread message

Podrigal, Aron

unread,
Feb 8, 2016, 11:19:02 PM2/8/16
to Django developers (Contributions to Django itself)
Hi,

While going through the code for ModelBase I came across the following which I was unable to get it  clear from the documentation.

A model inheriting from a concrete model does not inherit the Meta class of its parent no matter if it has a Meta of its own. However, it does inherit 2 options, which they are: ordering and get_latest_by. Even when it has a Meta of its own, as long as those 2 options have not been overridden explicitly. By contrast, inheriting from an abstract model inherits the Meta class of the abstract model when it does not have a Meta class of its own. What I find unclear, is, when a model inherits from an abstract model which defines meta.ordering, if the child model also has its own Meta class, it does not inherit the ordering option from it's parent. 

Is this by design


class AbstractModelWithOrdering(models.Model):
name = models.CharField(max_length=255)

class Meta:
ordering = ['-name']
abstract = True


class ExtendAbstractModelWithOrdering(AbstractModelWithOrdering):
class Meta:
db_table = 'extended_abstract_model'


class ConcreteModelWithOrdering(models.Model):
name = models.CharField(max_length=255)

class Meta:
ordering = ['-name']


class ExtendConcreteModelWithOrdering(ConcreteModelWithOrdering):
class Meta:
db_table = 'extended_concrete_model'



class InheritedOrderingTests(SimpleTestCase):
def test_inheriting_from_abstract_with_meta_inherits_ordering(self):
self.assertEqual(ExtendAbstractModelWithOrdering._meta.ordering, ['-name'])

def test_inheriting_from_concrete_with_meta_inherits_ordering(self):
self.assertEqual(ExtendConcreteModelWithOrdering._meta.ordering, ['-name'])


======================================================================
FAIL: test_inheriting_from_abstract_with_meta_inherits_ordering (model_meta.tests.InheritedOrderingTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/aron/django/django_repo/tests/model_meta/tests.py", line 280, in test_inheriting_from_abstract_with_meta_inherits_ordering
    self.assertEqual(ExtendAbstractModelWithOrdering._meta.ordering, ['-name'])
AssertionError: Lists differ: [] != ['-name']

Second list contains 1 additional elements.
First extra element 0:
-name

- []
+ ['-name']

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=1)


--
Aron Podrigal
-
'1000001', '1110010', '1101111', '1101110'   '1010000', '1101111', '1100100', '1110010', '1101001', '1100111', '1100001', '1101100'

P: '2b', '31', '33', '34', '37', '34', '35', '38', '36', '30', '39', '39'

Tim Graham

unread,
Feb 9, 2016, 10:29:22 AM2/9/16
to Django developers (Contributions to Django itself)
Did you try looking through git blame and old tickets to find the reason for it? The issue is raised on https://code.djangoproject.com/ticket/11078.

Podrigal, Aron

unread,
Feb 9, 2016, 11:20:48 AM2/9/16
to Django developers (Contributions to Django itself)
I see this was the intention from the very beginning as documented here [1], but wasn't my initial take. I guess it is debatable. In any case updating the documentation to clarify it would be helpful.



--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/83659928-1e62-469b-adb4-09dca6e15b6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages