2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2008-06-26 15:51:19 +08:00
|
|
|
import datetime
|
|
|
|
|
2008-06-09 22:03:35 +08:00
|
|
|
from django.db import models
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-06-09 22:03:35 +08:00
|
|
|
class Place(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
address = models.CharField(max_length=80)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
2008-06-26 15:51:19 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s the place" % self.name
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-06-09 22:03:35 +08:00
|
|
|
class Restaurant(Place):
|
2013-08-12 04:19:09 +08:00
|
|
|
serves_hot_dogs = models.BooleanField(default=False)
|
|
|
|
serves_pizza = models.BooleanField(default=False)
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s the restaurant" % self.name
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-06-09 22:03:35 +08:00
|
|
|
class ItalianRestaurant(Restaurant):
|
2013-08-12 04:19:09 +08:00
|
|
|
serves_gnocchi = models.BooleanField(default=False)
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s the italian restaurant" % self.name
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-06-09 22:03:35 +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)
|
2008-06-09 22:03:35 +08:00
|
|
|
capacity = models.IntegerField()
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s the parking lot" % self.name
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2009-03-04 15:21:14 +08:00
|
|
|
class ParkingLot3(Place):
|
|
|
|
# The parent_link connector need not be the pk on the model.
|
|
|
|
primary_key = models.AutoField(primary_key=True)
|
2015-07-22 22:43:21 +08:00
|
|
|
parent = models.OneToOneField(Place, models.CASCADE, parent_link=True)
|
2009-03-04 15:21:14 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2013-08-12 19:30:38 +08:00
|
|
|
class ParkingLot4(models.Model):
|
|
|
|
# Test parent_link connector can be discovered in abstract classes.
|
2015-07-22 22:43:21 +08:00
|
|
|
parent = models.OneToOneField(Place, models.CASCADE, parent_link=True)
|
2013-08-12 19:30:38 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2013-08-12 19:30:38 +08:00
|
|
|
class ParkingLot4A(ParkingLot4, Place):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2013-08-12 19:30:38 +08:00
|
|
|
class ParkingLot4B(Place, ParkingLot4):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2014-04-21 17:50:53 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-07-23 14:12:15 +08:00
|
|
|
class Supplier(models.Model):
|
2014-04-21 17:50:53 +08:00
|
|
|
name = models.CharField(max_length=50)
|
2015-07-22 22:43:21 +08:00
|
|
|
restaurant = models.ForeignKey(Restaurant, models.CASCADE)
|
2008-07-23 14:12:15 +08:00
|
|
|
|
2014-04-21 17:50:53 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2010-04-23 22:25:29 +08:00
|
|
|
class Wholesaler(Supplier):
|
2015-07-22 22:43:21 +08:00
|
|
|
retailer = models.ForeignKey(Supplier, models.CASCADE, related_name='wholesale_supplier')
|
2010-04-23 22:25:29 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-06-26 15:51:19 +08:00
|
|
|
class Parent(models.Model):
|
|
|
|
created = models.DateTimeField(default=datetime.datetime.now)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-06-26 15:51:19 +08:00
|
|
|
class Child(Parent):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-07-28 09:32:46 +08:00
|
|
|
class SelfRefParent(models.Model):
|
|
|
|
parent_data = models.IntegerField()
|
2015-07-22 22:43:21 +08:00
|
|
|
self_data = models.ForeignKey('self', models.SET_NULL, null=True)
|
2008-07-23 14:12:15 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-07-28 09:32:46 +08:00
|
|
|
class SelfRefChild(SelfRefParent):
|
|
|
|
child_data = models.IntegerField()
|
2008-07-23 14:12:15 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-09-02 05:04:01 +08:00
|
|
|
class Article(models.Model):
|
|
|
|
headline = models.CharField(max_length=100)
|
|
|
|
pub_date = models.DateTimeField()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2008-09-02 05:04:01 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ('-pub_date', 'headline')
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2008-09-02 05:04:01 +08:00
|
|
|
return self.headline
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-09-02 05:04:01 +08:00
|
|
|
class ArticleWithAuthor(Article):
|
2008-09-03 13:53:50 +08:00
|
|
|
author = models.CharField(max_length=100)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-09-03 13:53:50 +08:00
|
|
|
class M2MBase(models.Model):
|
|
|
|
articles = models.ManyToManyField(Article)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-09-03 13:53:50 +08:00
|
|
|
class M2MChild(M2MBase):
|
|
|
|
name = models.CharField(max_length=50)
|
2008-09-02 05:04:01 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-09-04 02:38:43 +08:00
|
|
|
class Evaluation(Article):
|
|
|
|
quality = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-09-04 02:38:43 +08:00
|
|
|
class QualityControl(Evaluation):
|
|
|
|
assignee = models.CharField(max_length=50)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-03-04 15:20:08 +08:00
|
|
|
class BaseM(models.Model):
|
|
|
|
base_name = models.CharField(max_length=100)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-03-04 15:20:08 +08:00
|
|
|
return self.base_name
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-03-04 15:20:08 +08:00
|
|
|
class DerivedM(BaseM):
|
|
|
|
customPK = models.IntegerField(primary_key=True)
|
|
|
|
derived_name = models.CharField(max_length=100)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2013-10-20 07:33:10 +08:00
|
|
|
return "PK = %d, base_name = %s, derived_name = %s" % (
|
|
|
|
self.customPK, self.base_name, self.derived_name)
|
2008-09-04 02:38:43 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2010-11-17 20:09:21 +08:00
|
|
|
class AuditBase(models.Model):
|
|
|
|
planned_date = models.DateField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2012-06-08 00:08:47 +08:00
|
|
|
verbose_name_plural = 'Audits'
|
2010-11-17 20:09:21 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2010-11-17 20:09:21 +08:00
|
|
|
class CertificationAudit(AuditBase):
|
|
|
|
class Meta(AuditBase.Meta):
|
|
|
|
abstract = True
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2010-11-17 20:09:21 +08:00
|
|
|
class InternalCertificationAudit(CertificationAudit):
|
|
|
|
auditing_dept = models.CharField(max_length=20)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2009-11-03 22:02:49 +08:00
|
|
|
# Check that abstract classes don't get m2m tables autocreated.
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-11-03 22:02:49 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-11-03 22:02:49 +08:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-11-03 22:02:49 +08:00
|
|
|
class AbstractEvent(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
attendees = models.ManyToManyField(Person, related_name="%(class)s_set")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-11-03 22:02:49 +08:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2009-11-03 22:02:49 +08:00
|
|
|
class BirthdayParty(AbstractEvent):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2009-11-03 22:02:49 +08:00
|
|
|
class BachelorParty(AbstractEvent):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2009-11-03 22:02:49 +08:00
|
|
|
class MessyBachelorParty(BachelorParty):
|
|
|
|
pass
|
2011-02-12 23:07:27 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2011-02-12 23:07:27 +08:00
|
|
|
# Check concrete -> abstract -> concrete inheritance
|
|
|
|
class SearchableLocation(models.Model):
|
|
|
|
keywords = models.CharField(max_length=256)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2011-02-12 23:07:27 +08:00
|
|
|
class Station(SearchableLocation):
|
|
|
|
name = models.CharField(max_length=128)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2011-02-12 23:07:27 +08:00
|
|
|
class BusStation(Station):
|
|
|
|
bus_routes = models.CommaSeparatedIntegerField(max_length=128)
|
2013-08-12 04:19:09 +08:00
|
|
|
inbound = models.BooleanField(default=False)
|
2011-02-12 23:07:27 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2011-02-12 23:07:27 +08:00
|
|
|
class TrainStation(Station):
|
|
|
|
zone = models.IntegerField()
|
2012-04-21 01:34:29 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-04-21 01:34:29 +08:00
|
|
|
class User(models.Model):
|
|
|
|
username = models.CharField(max_length=30, unique=True)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-04-21 01:34:29 +08:00
|
|
|
class Profile(User):
|
|
|
|
profile_id = models.AutoField(primary_key=True)
|
|
|
|
extra = models.CharField(max_length=30, blank=True)
|
2013-12-06 00:46:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Check concrete + concrete -> concrete -> concrete
|
|
|
|
class Politician(models.Model):
|
|
|
|
politician_id = models.AutoField(primary_key=True)
|
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
|
|
|
|
class Congressman(Person, Politician):
|
|
|
|
state = models.CharField(max_length=2)
|
|
|
|
|
|
|
|
|
|
|
|
class Senator(Congressman):
|
|
|
|
pass
|