diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2008-02-22 01:05:05 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2008-02-22 01:05:05 +0000 |
| commit | 0c20e88e65b8c2b1d097510ee2d7cfe6b2cf9b97 (patch) | |
| tree | 14378fabc83fb32b4a23941a856c0c690ef4aa5a /tests/modeltests/model_inheritance/models.py | |
| parent | ca123b0760de38b4df50ca321927a365d2d1c796 (diff) | |
queryset-refactor: Fixed up and documented Meta-class inheritance.
Should be mostly logical (for versions of "logical" that may require you to be
a Vulcan, admittedly, but that's not entirely my fault).
git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7141 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/modeltests/model_inheritance/models.py')
| -rw-r--r-- | tests/modeltests/model_inheritance/models.py | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/tests/modeltests/model_inheritance/models.py b/tests/modeltests/model_inheritance/models.py index 940b2ee10c..1187c5fe64 100644 --- a/tests/modeltests/model_inheritance/models.py +++ b/tests/modeltests/model_inheritance/models.py @@ -20,6 +20,7 @@ class CommonInfo(models.Model): class Meta: abstract = True + ordering = ['name'] def __unicode__(self): return u'%s %s' % (self.__class__.__name__, self.name) @@ -30,6 +31,9 @@ class Worker(CommonInfo): class Student(CommonInfo): school_class = models.CharField(max_length=10) + class Meta: + pass + class Place(models.Model): name = models.CharField(max_length=50) address = models.CharField(max_length=80) @@ -37,7 +41,13 @@ class Place(models.Model): def __unicode__(self): return u"%s the place" % self.name -class Restaurant(Place): +class Rating(models.Model): + rating = models.IntegerField(null=True, blank=True) + + class Meta: + abstract = True + +class Restaurant(Place, Rating): serves_hot_dogs = models.BooleanField() serves_pizza = models.BooleanField() @@ -71,6 +81,8 @@ __test__ = {'API_TESTS':""" >>> w = Worker(name='Fred', age=35, job='Quarry worker') >>> w.save() +>>> w2 = Worker(name='Barney', age=34, job='Quarry worker') +>>> w2.save() >>> s = Student(name='Pebbles', age=5, school_class='1B') >>> s.save() >>> unicode(w) @@ -78,6 +90,16 @@ u'Worker Fred' >>> unicode(s) u'Student Pebbles' +# The children inherit the Meta class of their parents (if they don't specify +# their own). +>>> Worker.objects.values('name') +[{'name': u'Barney'}, {'name': u'Fred'}] + +# Since Student does not subclass CommonInfo's Meta, it has the effect of +# completely overriding it. So ordering by name doesn't take place for Students. +>>> Student._meta.ordering +[] + # However, the CommonInfo class cannot be used as a normal model (it doesn't # exist as a model). >>> CommonInfo.objects.all() @@ -96,7 +118,7 @@ AttributeError: type object 'CommonInfo' has no attribute 'objects' >>> p2.save() Test constructor for Restaurant. ->>> r = Restaurant(name='Demon Dogs', address='944 W. Fullerton', serves_hot_dogs=True, serves_pizza=False) +>>> r = Restaurant(name='Demon Dogs', address='944 W. Fullerton',serves_hot_dogs=True, serves_pizza=False, rating=2) >>> r.save() # Test the constructor for ItalianRestaurant. @@ -106,9 +128,9 @@ Test constructor for Restaurant. # Make sure Restaurant and ItalianRestaurant have the right fields in the right # order. >>> [f.name for f in Restaurant._meta.fields] -['id', 'name', 'address', 'place_ptr', 'serves_hot_dogs', 'serves_pizza'] +['id', 'name', 'address', 'place_ptr', 'rating', 'serves_hot_dogs', 'serves_pizza'] >>> [f.name for f in ItalianRestaurant._meta.fields] -['id', 'name', 'address', 'place_ptr', 'serves_hot_dogs', 'serves_pizza', 'restaurant_ptr', 'serves_gnocchi'] +['id', 'name', 'address', 'place_ptr', 'rating', 'serves_hot_dogs', 'serves_pizza', 'restaurant_ptr', 'serves_gnocchi'] # Even though p.supplier for a Place 'p' (a parent of a Supplier), a Restaurant # object cannot access that reverse relation, since it's not part of the @@ -118,7 +140,7 @@ Test constructor for Restaurant. >>> Restaurant.objects.filter(supplier__name='foo') Traceback (most recent call last): ... -TypeError: Cannot resolve keyword 'supplier' into field. Choices are: address, id, italianrestaurant, lot, name, place_ptr, provider, serves_hot_dogs, serves_pizza +TypeError: Cannot resolve keyword 'supplier' into field. Choices are: address, id, italianrestaurant, lot, name, place_ptr, provider, rating, serves_hot_dogs, serves_pizza # Parent fields can be used directly in filters on the child model. >>> Restaurant.objects.filter(name='Demon Dogs') |
