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