2006-05-02 03:31:56 +02: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 04:50:16 +02: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 03:31:56 +02:00
|
|
|
"""
|
2012-06-07 18:08:47 +02:00
|
|
|
from __future__ import unicode_literals
|
2006-05-02 03:31:56 +02:00
|
|
|
|
|
|
|
from django.db import models
|
2012-08-12 12:32:08 +02:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2006-05-02 03:31:56 +02: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 04:50:16 +02:00
|
|
|
#
|
|
|
|
# Abstract base classes
|
|
|
|
#
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
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 04:50:16 +02:00
|
|
|
class CommonInfo(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
age = models.PositiveIntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
ordering = ['name']
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2012-06-07 18:08:47 +02: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 04:50:16 +02:00
|
|
|
|
2013-11-02 23:50:35 +01: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 04:50:16 +02:00
|
|
|
class Worker(CommonInfo):
|
|
|
|
job = models.CharField(max_length=50)
|
|
|
|
|
2013-11-02 23:50:35 +01: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 04:50:16 +02:00
|
|
|
class Student(CommonInfo):
|
|
|
|
school_class = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
pass
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2010-02-24 15:32:11 +01:00
|
|
|
class StudentWorker(Student, Worker):
|
|
|
|
pass
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2008-06-26 09:04:18 +02:00
|
|
|
#
|
|
|
|
# Abstract base classes with related models
|
|
|
|
#
|
|
|
|
|
|
|
|
class Post(models.Model):
|
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
2008-06-26 09:04:18 +02:00
|
|
|
class Attachment(models.Model):
|
|
|
|
post = models.ForeignKey(Post, related_name='attached_%(class)s_set')
|
|
|
|
content = models.TextField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2008-06-26 09:04:18 +02:00
|
|
|
return self.content
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2008-06-26 09:04:18 +02:00
|
|
|
class Comment(Attachment):
|
2013-08-11 22:19:09 +02:00
|
|
|
is_spam = models.BooleanField(default=False)
|
2008-06-26 09:04:18 +02:00
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2008-06-26 09:04:18 +02:00
|
|
|
class Link(Attachment):
|
|
|
|
url = models.URLField()
|
|
|
|
|
2013-11-02 23:50:35 +01: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 04:50:16 +02:00
|
|
|
#
|
|
|
|
# Multi-table inheritance
|
|
|
|
#
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
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 04:50:16 +02:00
|
|
|
class Chef(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2012-06-07 18:08:47 +02:00
|
|
|
return "%s the chef" % 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 04:50:16 +02:00
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
2006-05-02 03:31:56 +02:00
|
|
|
class Place(models.Model):
|
2007-08-05 07:14:46 +02:00
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
address = models.CharField(max_length=80)
|
2006-05-02 03:31:56 +02:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2012-06-07 18:08:47 +02:00
|
|
|
return "%s the place" % self.name
|
2006-05-02 03:31:56 +02:00
|
|
|
|
2013-11-02 23:50:35 +01: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 04:50:16 +02:00
|
|
|
class Rating(models.Model):
|
|
|
|
rating = models.IntegerField(null=True, blank=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
ordering = ['-rating']
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
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 04:50:16 +02:00
|
|
|
class Restaurant(Place, Rating):
|
2013-08-11 22:19:09 +02:00
|
|
|
serves_hot_dogs = models.BooleanField(default=False)
|
|
|
|
serves_pizza = models.BooleanField(default=False)
|
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 04:50:16 +02:00
|
|
|
chef = models.ForeignKey(Chef, null=True, blank=True)
|
|
|
|
|
|
|
|
class Meta(Rating.Meta):
|
|
|
|
db_table = 'my_restaurant'
|
2006-05-02 03:31:56 +02:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2012-06-07 18:08:47 +02:00
|
|
|
return "%s the restaurant" % self.name
|
2006-05-02 03:31:56 +02:00
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
2006-05-02 03:31:56 +02:00
|
|
|
class ItalianRestaurant(Restaurant):
|
2013-08-11 22:19:09 +02:00
|
|
|
serves_gnocchi = models.BooleanField(default=False)
|
2006-05-02 03:31:56 +02:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2012-06-07 18:08:47 +02:00
|
|
|
return "%s the italian restaurant" % self.name
|
2006-05-02 03:31:56 +02:00
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
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 04:50:16 +02:00
|
|
|
class Supplier(Place):
|
|
|
|
customers = models.ManyToManyField(Restaurant, related_name='provider')
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2012-06-07 18:08:47 +02:00
|
|
|
return "%s the supplier" % 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 04:50:16 +02:00
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
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 04:50:16 +02:00
|
|
|
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')
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2012-06-07 18:08:47 +02:00
|
|
|
return "%s the parking lot" % 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 04:50:16 +02:00
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2010-01-09 21:03:52 +01: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)
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2010-01-09 21:03:52 +01:00
|
|
|
class NamedURL(models.Model):
|
|
|
|
title = models.ForeignKey(Title, related_name='attached_%(app_label)s_%(class)s_set')
|
|
|
|
url = models.URLField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
2010-01-09 21:03:52 +01:00
|
|
|
class Copy(NamedURL):
|
|
|
|
content = models.TextField()
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2010-01-09 21:03:52 +01:00
|
|
|
return self.content
|
2011-01-26 04:42:31 +01:00
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2011-01-26 04:42:31 +01:00
|
|
|
class Mixin(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.other_attr = 1
|
|
|
|
super(Mixin, self).__init__()
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2011-01-26 04:42:31 +01:00
|
|
|
class MixinModel(models.Model, Mixin):
|
|
|
|
pass
|
2013-08-20 16:13:41 +02:00
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2013-08-20 16:13:41 +02:00
|
|
|
class Base(models.Model):
|
|
|
|
titles = models.ManyToManyField(Title)
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2013-08-20 16:13:41 +02:00
|
|
|
class SubBase(Base):
|
|
|
|
sub_id = models.IntegerField(primary_key=True)
|