Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/model_inheritance_regress/models.py
blob: 811c8175bb71ab5f3c1c5e878c8f3a15e4d4cd32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from __future__ import unicode_literals

import datetime

from django.db import models
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class Place(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=80)

    class Meta:
        ordering = ('name',)

    def __str__(self):
        return "%s the place" % self.name

@python_2_unicode_compatible
class Restaurant(Place):
    serves_hot_dogs = models.BooleanField()
    serves_pizza = models.BooleanField()

    def __str__(self):
        return "%s the restaurant" % self.name

@python_2_unicode_compatible
class ItalianRestaurant(Restaurant):
    serves_gnocchi = models.BooleanField()

    def __str__(self):
        return "%s the italian restaurant" % self.name

@python_2_unicode_compatible
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)
    capacity = models.IntegerField()

    def __str__(self):
        return "%s the parking lot" % self.name

class ParkingLot2(Place):
    # In lieu of any other connector, an existing OneToOneField will be
    # promoted to the primary key.
    parent = models.OneToOneField(Place)

class ParkingLot3(Place):
    # The parent_link connector need not be the pk on the model.
    primary_key = models.AutoField(primary_key=True)
    parent = models.OneToOneField(Place, parent_link=True)

class Supplier(models.Model):
    restaurant = models.ForeignKey(Restaurant)

class Wholesaler(Supplier):
    retailer = models.ForeignKey(Supplier,related_name='wholesale_supplier')

class Parent(models.Model):
    created = models.DateTimeField(default=datetime.datetime.now)

class Child(Parent):
    name = models.CharField(max_length=10)

class SelfRefParent(models.Model):
    parent_data = models.IntegerField()
    self_data = models.ForeignKey('self', null=True)

class SelfRefChild(SelfRefParent):
    child_data = models.IntegerField()

@python_2_unicode_compatible
class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateTimeField()
    class Meta:
        ordering = ('-pub_date', 'headline')

    def __str__(self):
        return self.headline

class ArticleWithAuthor(Article):
    author = models.CharField(max_length=100)

class M2MBase(models.Model):
    articles = models.ManyToManyField(Article)

class M2MChild(M2MBase):
    name = models.CharField(max_length=50)

class Evaluation(Article):
    quality = models.IntegerField()

    class Meta:
        abstract = True

class QualityControl(Evaluation):
    assignee = models.CharField(max_length=50)

@python_2_unicode_compatible
class BaseM(models.Model):
    base_name = models.CharField(max_length=100)

    def __str__(self):
        return self.base_name

@python_2_unicode_compatible
class DerivedM(BaseM):
    customPK = models.IntegerField(primary_key=True)
    derived_name = models.CharField(max_length=100)

    def __str__(self):
        return "PK = %d, base_name = %s, derived_name = %s" \
                % (self.customPK, self.base_name, self.derived_name)

class AuditBase(models.Model):
    planned_date = models.DateField()

    class Meta:
        abstract = True
        verbose_name_plural = 'Audits'

class CertificationAudit(AuditBase):
    class Meta(AuditBase.Meta):
        abstract = True

class InternalCertificationAudit(CertificationAudit):
    auditing_dept = models.CharField(max_length=20)

# Check that abstract classes don't get m2m tables autocreated.
@python_2_unicode_compatible
class Person(models.Model):
    name = models.CharField(max_length=100)

    class Meta:
        ordering = ('name',)

    def __str__(self):
        return self.name

@python_2_unicode_compatible
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',)

    def __str__(self):
        return self.name

class BirthdayParty(AbstractEvent):
    pass

class BachelorParty(AbstractEvent):
    pass

class MessyBachelorParty(BachelorParty):
    pass

# Check concrete -> abstract -> concrete inheritance
class SearchableLocation(models.Model):
    keywords = models.CharField(max_length=256)

class Station(SearchableLocation):
    name = models.CharField(max_length=128)

    class Meta:
        abstract = True

class BusStation(Station):
    bus_routes = models.CommaSeparatedIntegerField(max_length=128)
    inbound = models.BooleanField()

class TrainStation(Station):
    zone = models.IntegerField()

class User(models.Model):
    username = models.CharField(max_length=30, unique=True)

class Profile(User):
    profile_id = models.AutoField(primary_key=True)
    extra = models.CharField(max_length=30, blank=True)