2008-09-01 21:28:32 +00:00
|
|
|
import datetime
|
2015-02-20 19:28:34 +00:00
|
|
|
import uuid
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
class Author(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
2008-07-26 14:49:39 +00:00
|
|
|
class Meta:
|
|
|
|
ordering = ("name",)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-07-18 23:54:34 +00:00
|
|
|
return self.name
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-08-25 03:51:25 +00:00
|
|
|
class BetterAuthor(Author):
|
|
|
|
write_speed = models.IntegerField()
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
class Book(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
author = models.ForeignKey(Author, models.CASCADE)
|
2008-07-18 23:54:34 +00:00
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
|
2009-05-07 12:17:52 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = (("author", "title"),)
|
|
|
|
ordering = ["id"]
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-07-18 23:54:34 +00:00
|
|
|
return self.title
|
2009-03-30 15:58:52 +00:00
|
|
|
|
2015-09-19 12:23:55 +00:00
|
|
|
def clean(self):
|
|
|
|
# Ensure author is always accessible in clean method
|
|
|
|
assert self.author.name is not None
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-12-21 04:56:32 +00:00
|
|
|
class BookWithCustomPK(models.Model):
|
|
|
|
my_pk = models.DecimalField(max_digits=5, decimal_places=0, primary_key=True)
|
2015-07-22 14:43:21 +00:00
|
|
|
author = models.ForeignKey(Author, models.CASCADE)
|
2008-12-21 04:56:32 +00:00
|
|
|
title = models.CharField(max_length=100)
|
2008-07-18 23:54:34 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2012-06-07 16:08:47 +00:00
|
|
|
return "%s: %s" % (self.my_pk, self.title)
|
2009-03-30 15:58:52 +00:00
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2010-10-13 00:30:02 +00:00
|
|
|
class Editor(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2010-10-13 00:30:02 +00:00
|
|
|
class BookWithOptionalAltEditor(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
author = models.ForeignKey(Author, models.CASCADE)
|
2010-10-13 00:30:02 +00:00
|
|
|
# Optional secondary author
|
2015-07-22 14:43:21 +00:00
|
|
|
alt_editor = models.ForeignKey(Editor, models.SET_NULL, blank=True, null=True)
|
2010-10-13 00:30:02 +00:00
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (("author", "title", "alt_editor"),)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2010-10-13 00:30:02 +00:00
|
|
|
return self.title
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2009-02-03 14:02:09 +00:00
|
|
|
class AlternateBook(Book):
|
|
|
|
notes = models.CharField(max_length=100)
|
2009-03-30 15:58:52 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2012-06-07 16:08:47 +00:00
|
|
|
return "%s - %s" % (self.title, self.notes)
|
2009-03-30 15:58:52 +00:00
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
class AuthorMeeting(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
authors = models.ManyToManyField(Author)
|
|
|
|
created = models.DateField(editable=False)
|
2008-07-27 01:18:18 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-07-18 23:54:34 +00:00
|
|
|
return self.name
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-08-01 20:35:40 +00:00
|
|
|
class CustomPrimaryKey(models.Model):
|
|
|
|
my_pk = models.CharField(max_length=10, primary_key=True)
|
|
|
|
some_field = models.CharField(max_length=100)
|
|
|
|
|
2008-07-18 23:54:34 +00:00
|
|
|
|
2008-08-29 17:41:40 +00:00
|
|
|
# models for inheritance tests.
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-08-29 17:41:40 +00:00
|
|
|
class Place(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
city = models.CharField(max_length=50)
|
2009-05-07 12:17:52 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-08-29 17:41:40 +00:00
|
|
|
return self.name
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-08-29 17:41:40 +00:00
|
|
|
class Owner(models.Model):
|
2008-08-31 09:49:55 +00:00
|
|
|
auto_id = models.AutoField(primary_key=True)
|
2008-08-29 17:41:40 +00:00
|
|
|
name = models.CharField(max_length=100)
|
2015-07-22 14:43:21 +00:00
|
|
|
place = models.ForeignKey(Place, models.CASCADE)
|
2009-03-30 15:58:52 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-08-31 09:49:55 +00:00
|
|
|
return "%s at %s" % (self.name, self.place)
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-08-31 21:14:46 +00:00
|
|
|
class Location(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
place = models.ForeignKey(Place, models.CASCADE, unique=True)
|
2008-08-31 21:14:46 +00:00
|
|
|
# this is purely for testing the data doesn't matter here :)
|
|
|
|
lat = models.CharField(max_length=100)
|
|
|
|
lon = models.CharField(max_length=100)
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-08-31 09:49:55 +00:00
|
|
|
class OwnerProfile(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
owner = models.OneToOneField(Owner, models.CASCADE, primary_key=True)
|
2008-08-31 09:49:55 +00:00
|
|
|
age = models.PositiveIntegerField()
|
2009-03-30 15:58:52 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-08-31 09:49:55 +00:00
|
|
|
return "%s is %d" % (self.owner.name, self.age)
|
2008-08-29 17:41:40 +00:00
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-08-29 17:41:40 +00:00
|
|
|
class Restaurant(Place):
|
2013-08-11 20:19:09 +00:00
|
|
|
serves_pizza = models.BooleanField(default=False)
|
2009-05-07 12:17:52 +00:00
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-09-01 19:08:08 +00:00
|
|
|
class Product(models.Model):
|
|
|
|
slug = models.SlugField(unique=True)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-09-01 19:08:08 +00:00
|
|
|
return self.slug
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-09-01 19:08:08 +00:00
|
|
|
class Price(models.Model):
|
|
|
|
price = models.DecimalField(max_digits=10, decimal_places=2)
|
|
|
|
quantity = models.PositiveIntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (("price", "quantity"),)
|
|
|
|
|
2018-12-28 00:34:14 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "%s for %s" % (self.quantity, self.price)
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-08-29 17:41:40 +00:00
|
|
|
class MexicanRestaurant(Restaurant):
|
2013-08-11 20:19:09 +00:00
|
|
|
serves_tacos = models.BooleanField(default=False)
|
2008-08-29 17:41:40 +00:00
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2009-04-18 21:03:29 +00:00
|
|
|
class ClassyMexicanRestaurant(MexicanRestaurant):
|
2016-03-18 14:24:29 +00:00
|
|
|
the_restaurant = models.OneToOneField(
|
|
|
|
MexicanRestaurant, models.CASCADE, parent_link=True, primary_key=True
|
|
|
|
)
|
2013-08-11 20:19:09 +00:00
|
|
|
tacos_are_yummy = models.BooleanField(default=False)
|
2009-04-18 21:03:29 +00:00
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-10-31 22:07:05 +00:00
|
|
|
# models for testing unique_together validation when a fk is involved and
|
|
|
|
# using inlineformset_factory.
|
|
|
|
class Repository(models.Model):
|
|
|
|
name = models.CharField(max_length=25)
|
2009-03-30 15:58:52 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-10-31 22:07:05 +00:00
|
|
|
return self.name
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-10-31 22:07:05 +00:00
|
|
|
class Revision(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
repository = models.ForeignKey(Repository, models.CASCADE)
|
2008-10-31 22:07:05 +00:00
|
|
|
revision = models.CharField(max_length=40)
|
2009-03-30 15:58:52 +00:00
|
|
|
|
2008-10-31 22:07:05 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = (("repository", "revision"),)
|
2009-03-30 15:58:52 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2016-12-29 15:27:49 +00:00
|
|
|
return "%s (%s)" % (self.revision, str(self.repository))
|
2008-10-31 22:07:05 +00:00
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-09-01 21:28:32 +00:00
|
|
|
# models for testing callable defaults (see bug #7975). If you define a model
|
|
|
|
# with a callable default value, you cannot rely on the initial value in a
|
|
|
|
# form.
|
|
|
|
class Person(models.Model):
|
|
|
|
name = models.CharField(max_length=128)
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-09-01 21:28:32 +00:00
|
|
|
class Membership(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
person = models.ForeignKey(Person, models.CASCADE)
|
2008-09-01 21:28:32 +00:00
|
|
|
date_joined = models.DateTimeField(default=datetime.datetime.now)
|
|
|
|
karma = models.IntegerField()
|
2008-08-29 17:41:40 +00:00
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-10-28 20:01:03 +00:00
|
|
|
# models for testing a null=True fk to a parent
|
|
|
|
class Team(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2008-10-28 20:01:03 +00:00
|
|
|
class Player(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
team = models.ForeignKey(Team, models.SET_NULL, null=True)
|
2008-10-28 20:01:03 +00:00
|
|
|
name = models.CharField(max_length=100)
|
2009-03-30 15:58:52 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2009-03-30 15:58:52 +00:00
|
|
|
return self.name
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2009-03-30 15:58:52 +00:00
|
|
|
# Models for testing custom ModelForm save methods in formsets and inline formsets
|
|
|
|
class Poet(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2009-03-30 15:58:52 +00:00
|
|
|
return self.name
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2009-03-30 15:58:52 +00:00
|
|
|
class Poem(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
poet = models.ForeignKey(Poet, models.CASCADE)
|
2009-03-30 15:58:52 +00:00
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-10-28 20:01:03 +00:00
|
|
|
return self.name
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2009-05-07 12:17:52 +00:00
|
|
|
class Post(models.Model):
|
|
|
|
title = models.CharField(max_length=50, unique_for_date="posted", blank=True)
|
|
|
|
slug = models.CharField(max_length=50, unique_for_year="posted", blank=True)
|
|
|
|
subtitle = models.CharField(max_length=50, unique_for_month="posted", blank=True)
|
|
|
|
posted = models.DateField()
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2018-07-30 20:03:52 +00:00
|
|
|
return self.title
|
2015-02-20 19:28:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Models for testing UUID primary keys
|
|
|
|
class UUIDPKParent(models.Model):
|
|
|
|
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
|
|
|
|
class UUIDPKChild(models.Model):
|
|
|
|
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
|
|
name = models.CharField(max_length=255)
|
2015-07-22 14:43:21 +00:00
|
|
|
parent = models.ForeignKey(UUIDPKParent, models.CASCADE)
|
2015-06-05 20:04:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ChildWithEditablePK(models.Model):
|
|
|
|
name = models.CharField(max_length=255, primary_key=True)
|
2015-07-22 14:43:21 +00:00
|
|
|
parent = models.ForeignKey(UUIDPKParent, models.CASCADE)
|
2015-06-05 20:04:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AutoPKChildOfUUIDPKParent(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2015-07-22 14:43:21 +00:00
|
|
|
parent = models.ForeignKey(UUIDPKParent, models.CASCADE)
|
2015-06-05 20:04:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AutoPKParent(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
|
|
|
|
class UUIDPKChildOfAutoPKParent(models.Model):
|
|
|
|
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
|
|
name = models.CharField(max_length=255)
|
2015-07-22 14:43:21 +00:00
|
|
|
parent = models.ForeignKey(AutoPKParent, models.CASCADE)
|
2015-06-05 20:04:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ParentWithUUIDAlternateKey(models.Model):
|
|
|
|
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
|
|
|
|
class ChildRelatedViaAK(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2015-07-22 14:43:21 +00:00
|
|
|
parent = models.ForeignKey(
|
|
|
|
ParentWithUUIDAlternateKey, models.CASCADE, to_field="uuid"
|
|
|
|
)
|