2008-09-02 05:28:32 +08:00
|
|
|
import datetime
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Author(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
2008-07-26 22:49:39 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
2008-08-25 11:51:25 +08:00
|
|
|
class BetterAuthor(Author):
|
|
|
|
write_speed = models.IntegerField()
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class Book(models.Model):
|
|
|
|
author = models.ForeignKey(Author)
|
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
|
2009-05-07 20:17:52 +08:00
|
|
|
class Meta:
|
|
|
|
unique_together = (
|
|
|
|
('author', 'title'),
|
|
|
|
)
|
|
|
|
ordering = ['id']
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
2009-03-30 23:58:52 +08:00
|
|
|
|
2008-12-21 12:56:32 +08:00
|
|
|
class BookWithCustomPK(models.Model):
|
|
|
|
my_pk = models.DecimalField(max_digits=5, decimal_places=0, primary_key=True)
|
|
|
|
author = models.ForeignKey(Author)
|
|
|
|
title = models.CharField(max_length=100)
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2008-12-21 12:56:32 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u'%s: %s' % (self.my_pk, self.title)
|
2009-03-30 23:58:52 +08:00
|
|
|
|
2010-10-13 08:30:02 +08:00
|
|
|
class Editor(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
class BookWithOptionalAltEditor(models.Model):
|
|
|
|
author = models.ForeignKey(Author)
|
|
|
|
# Optional secondary author
|
|
|
|
alt_editor = models.ForeignKey(Editor, blank=True, null=True)
|
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (
|
|
|
|
('author', 'title', 'alt_editor'),
|
|
|
|
)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
|
|
|
|
2009-02-03 22:02:09 +08:00
|
|
|
class AlternateBook(Book):
|
|
|
|
notes = models.CharField(max_length=100)
|
2009-03-30 23:58:52 +08:00
|
|
|
|
2009-02-03 22:02:09 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u'%s - %s' % (self.title, self.notes)
|
2009-03-30 23:58:52 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class AuthorMeeting(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
authors = models.ManyToManyField(Author)
|
|
|
|
created = models.DateField(editable=False)
|
2008-07-27 09:18:18 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
2008-08-02 04:35:40 +08:00
|
|
|
class CustomPrimaryKey(models.Model):
|
|
|
|
my_pk = models.CharField(max_length=10, primary_key=True)
|
|
|
|
some_field = models.CharField(max_length=100)
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2008-08-30 01:41:40 +08:00
|
|
|
# models for inheritance tests.
|
|
|
|
|
|
|
|
class Place(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
city = models.CharField(max_length=50)
|
2009-05-07 20:17:52 +08:00
|
|
|
|
2008-08-30 01:41:40 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Owner(models.Model):
|
2008-08-31 17:49:55 +08:00
|
|
|
auto_id = models.AutoField(primary_key=True)
|
2008-08-30 01:41:40 +08:00
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
place = models.ForeignKey(Place)
|
2009-03-30 23:58:52 +08:00
|
|
|
|
2008-08-31 17:49:55 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return "%s at %s" % (self.name, self.place)
|
|
|
|
|
2008-09-01 05:14:46 +08:00
|
|
|
class Location(models.Model):
|
|
|
|
place = models.ForeignKey(Place, unique=True)
|
|
|
|
# this is purely for testing the data doesn't matter here :)
|
|
|
|
lat = models.CharField(max_length=100)
|
|
|
|
lon = models.CharField(max_length=100)
|
|
|
|
|
2008-08-31 17:49:55 +08:00
|
|
|
class OwnerProfile(models.Model):
|
|
|
|
owner = models.OneToOneField(Owner, primary_key=True)
|
|
|
|
age = models.PositiveIntegerField()
|
2009-03-30 23:58:52 +08:00
|
|
|
|
2008-08-31 17:49:55 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return "%s is %d" % (self.owner.name, self.age)
|
2008-08-30 01:41:40 +08:00
|
|
|
|
|
|
|
class Restaurant(Place):
|
|
|
|
serves_pizza = models.BooleanField()
|
2009-05-07 20:17:52 +08:00
|
|
|
|
2008-08-30 01:41:40 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
2008-09-02 03:08:08 +08:00
|
|
|
class Product(models.Model):
|
|
|
|
slug = models.SlugField(unique=True)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.slug
|
|
|
|
|
|
|
|
class Price(models.Model):
|
|
|
|
price = models.DecimalField(max_digits=10, decimal_places=2)
|
|
|
|
quantity = models.PositiveIntegerField()
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s for %s" % (self.quantity, self.price)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (('price', 'quantity'),)
|
|
|
|
|
2008-08-30 01:41:40 +08:00
|
|
|
class MexicanRestaurant(Restaurant):
|
|
|
|
serves_tacos = models.BooleanField()
|
|
|
|
|
2009-04-19 05:03:29 +08:00
|
|
|
class ClassyMexicanRestaurant(MexicanRestaurant):
|
|
|
|
restaurant = models.OneToOneField(MexicanRestaurant, parent_link=True, primary_key=True)
|
|
|
|
tacos_are_yummy = models.BooleanField()
|
|
|
|
|
2008-11-01 06:07:05 +08: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 23:58:52 +08:00
|
|
|
|
2008-11-01 06:07:05 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Revision(models.Model):
|
|
|
|
repository = models.ForeignKey(Repository)
|
|
|
|
revision = models.CharField(max_length=40)
|
2009-03-30 23:58:52 +08:00
|
|
|
|
2008-11-01 06:07:05 +08:00
|
|
|
class Meta:
|
|
|
|
unique_together = (("repository", "revision"),)
|
2009-03-30 23:58:52 +08:00
|
|
|
|
2008-11-01 06:07:05 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s (%s)" % (self.revision, unicode(self.repository))
|
|
|
|
|
2008-09-02 05:28:32 +08: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)
|
|
|
|
|
|
|
|
class Membership(models.Model):
|
|
|
|
person = models.ForeignKey(Person)
|
|
|
|
date_joined = models.DateTimeField(default=datetime.datetime.now)
|
|
|
|
karma = models.IntegerField()
|
2008-08-30 01:41:40 +08:00
|
|
|
|
2008-10-29 04:01:03 +08:00
|
|
|
# models for testing a null=True fk to a parent
|
|
|
|
class Team(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
class Player(models.Model):
|
|
|
|
team = models.ForeignKey(Team, null=True)
|
|
|
|
name = models.CharField(max_length=100)
|
2009-03-30 23:58:52 +08:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
# Models for testing custom ModelForm save methods in formsets and inline formsets
|
|
|
|
class Poet(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Poem(models.Model):
|
|
|
|
poet = models.ForeignKey(Poet)
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
2008-10-29 04:01:03 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
2009-05-07 20:17:52 +08: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()
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|