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
|
|
|
|
#
|
|
|
|
|
2013-11-03 06:50:35 +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 CommonInfo(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
age = models.PositiveIntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
ordering = ["name"]
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s %s" % (self.__class__.__name__, self.name)
|
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
|
|
|
|
2013-11-03 06:50:35 +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 Worker(CommonInfo):
|
|
|
|
job = models.CharField(max_length=50)
|
|
|
|
|
2013-11-03 06:50:35 +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 Student(CommonInfo):
|
|
|
|
school_class = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
pass
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-06-26 15:04:18 +08:00
|
|
|
#
|
|
|
|
# Abstract base classes with related models
|
|
|
|
#
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2008-06-26 15:04:18 +08:00
|
|
|
class Post(models.Model):
|
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-06-26 15:04:18 +08:00
|
|
|
class Attachment(models.Model):
|
2015-09-30 01:52:26 +08:00
|
|
|
post = models.ForeignKey(
|
|
|
|
Post,
|
|
|
|
models.CASCADE,
|
|
|
|
related_name="attached_%(class)s_set",
|
|
|
|
related_query_name="attached_%(app_label)s_%(class)ss",
|
|
|
|
)
|
2008-06-26 15:04:18 +08:00
|
|
|
content = models.TextField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-06-26 15:04:18 +08:00
|
|
|
class Comment(Attachment):
|
2013-08-12 04:19:09 +08:00
|
|
|
is_spam = models.BooleanField(default=False)
|
2008-06-26 15:04:18 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-06-26 15:04:18 +08:00
|
|
|
class Link(Attachment):
|
|
|
|
url = models.URLField()
|
|
|
|
|
2013-11-03 06:50:35 +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
|
|
|
#
|
|
|
|
# Multi-table inheritance
|
|
|
|
#
|
|
|
|
|
2022-02-04 03:24:19 +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 Chef(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
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
|
|
|
|
2013-11-03 06:50:35 +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"]
|
|
|
|
|
2013-11-03 06:50:35 +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 Restaurant(Place, Rating):
|
2013-08-12 04:19:09 +08:00
|
|
|
serves_hot_dogs = models.BooleanField(default=False)
|
|
|
|
serves_pizza = models.BooleanField(default=False)
|
2015-07-22 22:43:21 +08:00
|
|
|
chef = models.ForeignKey(Chef, models.SET_NULL, null=True, blank=True)
|
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 Meta(Rating.Meta):
|
|
|
|
db_table = "my_restaurant"
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class ItalianRestaurant(Restaurant):
|
2013-08-12 04:19:09 +08:00
|
|
|
serves_gnocchi = models.BooleanField(default=False)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 06:50:35 +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")
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2022-11-02 19:30:16 +08:00
|
|
|
class CustomSupplier(Supplier):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
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 ParkingLot(Place):
|
|
|
|
# An explicit link to the parent (we can control the attribute name).
|
2015-07-22 22:43:21 +08:00
|
|
|
parent = models.OneToOneField(
|
|
|
|
Place, models.CASCADE, primary_key=True, parent_link=True
|
|
|
|
)
|
|
|
|
main_site = models.ForeignKey(Place, models.CASCADE, related_name="lot")
|
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
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
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
|
|
|
|
#
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-01-10 04:03:52 +08:00
|
|
|
class Title(models.Model):
|
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2010-01-10 04:03:52 +08:00
|
|
|
class NamedURL(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
title = models.ForeignKey(
|
|
|
|
Title, models.CASCADE, related_name="attached_%(app_label)s_%(class)s_set"
|
|
|
|
)
|
2010-01-10 04:03:52 +08:00
|
|
|
url = models.URLField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Mixin:
|
2011-01-26 11:42:31 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.other_attr = 1
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__()
|
2011-01-26 11:42:31 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2011-01-26 11:42:31 +08:00
|
|
|
class MixinModel(models.Model, Mixin):
|
|
|
|
pass
|
2013-08-20 22:13:41 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2013-08-20 22:13:41 +08:00
|
|
|
class Base(models.Model):
|
|
|
|
titles = models.ManyToManyField(Title)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2013-08-20 22:13:41 +08:00
|
|
|
class SubBase(Base):
|
|
|
|
sub_id = models.IntegerField(primary_key=True)
|
2015-01-22 11:15:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GrandParent(models.Model):
|
|
|
|
first_name = models.CharField(max_length=80)
|
|
|
|
last_name = models.CharField(max_length=80)
|
|
|
|
email = models.EmailField(unique=True)
|
2017-11-23 15:14:32 +08:00
|
|
|
place = models.ForeignKey(Place, models.CASCADE, null=True, related_name="+")
|
2015-01-22 11:15:59 +08:00
|
|
|
|
|
|
|
class Meta:
|
2020-02-19 12:09:48 +08:00
|
|
|
# Ordering used by test_inherited_ordering_pk_desc.
|
|
|
|
ordering = ["-pk"]
|
2015-01-22 11:15:59 +08:00
|
|
|
unique_together = ("first_name", "last_name")
|
|
|
|
|
|
|
|
|
|
|
|
class Parent(GrandParent):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Child(Parent):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class GrandChild(Child):
|
|
|
|
pass
|