2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
XX. Model inheritance
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
Model inheritance exists in two varieties:
|
|
|
|
- abstract base classes which are a way of specifying common
|
|
|
|
information inherited by the subclasses. They don't exist as a separate
|
|
|
|
model.
|
|
|
|
- non-abstract base classes (the default), which are models in their own
|
|
|
|
right with their own database tables and everything. Their subclasses
|
|
|
|
have references back to them, created automatically.
|
|
|
|
|
|
|
|
Both styles are demonstrated here.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
#
|
|
|
|
# Abstract base classes
|
|
|
|
#
|
|
|
|
|
|
|
|
class CommonInfo(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
age = models.PositiveIntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
ordering = ['name']
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u'%s %s' % (self.__class__.__name__, self.name)
|
|
|
|
|
|
|
|
class Worker(CommonInfo):
|
|
|
|
job = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
class Student(CommonInfo):
|
|
|
|
school_class = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
pass
|
|
|
|
|
2010-02-24 22:32:11 +08:00
|
|
|
class StudentWorker(Student, Worker):
|
|
|
|
pass
|
|
|
|
|
2008-06-26 15:04:18 +08:00
|
|
|
#
|
|
|
|
# Abstract base classes with related models
|
|
|
|
#
|
|
|
|
|
|
|
|
class Post(models.Model):
|
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
class Attachment(models.Model):
|
|
|
|
post = models.ForeignKey(Post, related_name='attached_%(class)s_set')
|
|
|
|
content = models.TextField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.content
|
|
|
|
|
|
|
|
class Comment(Attachment):
|
|
|
|
is_spam = models.BooleanField()
|
|
|
|
|
|
|
|
class Link(Attachment):
|
|
|
|
url = models.URLField()
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
#
|
|
|
|
# Multi-table inheritance
|
|
|
|
#
|
|
|
|
|
|
|
|
class Chef(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s the chef" % self.name
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Place(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
address = models.CharField(max_length=80)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s the place" % self.name
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
class Rating(models.Model):
|
|
|
|
rating = models.IntegerField(null=True, blank=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
ordering = ['-rating']
|
|
|
|
|
|
|
|
class Restaurant(Place, Rating):
|
2006-05-02 09:31:56 +08:00
|
|
|
serves_hot_dogs = models.BooleanField()
|
|
|
|
serves_pizza = models.BooleanField()
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
chef = models.ForeignKey(Chef, null=True, blank=True)
|
|
|
|
|
|
|
|
class Meta(Rating.Meta):
|
|
|
|
db_table = 'my_restaurant'
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s the restaurant" % self.name
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
class ItalianRestaurant(Restaurant):
|
|
|
|
serves_gnocchi = models.BooleanField()
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s the italian restaurant" % self.name
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
class Supplier(Place):
|
|
|
|
customers = models.ManyToManyField(Restaurant, related_name='provider')
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s the supplier" % self.name
|
|
|
|
|
|
|
|
class ParkingLot(Place):
|
|
|
|
# An explicit link to the parent (we can control the attribute name).
|
|
|
|
parent = models.OneToOneField(Place, primary_key=True, parent_link=True)
|
|
|
|
main_site = models.ForeignKey(Place, related_name='lot')
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s the parking lot" % self.name
|
|
|
|
|
2010-01-10 04:03:52 +08:00
|
|
|
#
|
|
|
|
# Abstract base classes with related models where the sub-class has the
|
|
|
|
# same name in a different app and inherits from the same abstract base
|
|
|
|
# class.
|
|
|
|
# NOTE: The actual API tests for the following classes are in
|
|
|
|
# model_inheritance_same_model_name/models.py - They are defined
|
|
|
|
# here in order to have the name conflict between apps
|
|
|
|
#
|
|
|
|
|
|
|
|
class Title(models.Model):
|
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
class NamedURL(models.Model):
|
|
|
|
title = models.ForeignKey(Title, related_name='attached_%(app_label)s_%(class)s_set')
|
|
|
|
url = models.URLField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
class Copy(NamedURL):
|
|
|
|
content = models.TextField()
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.content
|