2009-12-20 10:46:58 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2009-12-20 10:46:58 +08:00
|
|
|
class Author(models.Model):
|
|
|
|
first_name = models.CharField(max_length=255)
|
|
|
|
last_name = models.CharField(max_length=255)
|
|
|
|
dob = models.DateField()
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Author, self).__init__(*args, **kwargs)
|
|
|
|
# Protect against annotations being passed to __init__ --
|
|
|
|
# this'll make the test suite get angry if annotations aren't
|
|
|
|
# treated differently than fields.
|
|
|
|
for k in kwargs:
|
|
|
|
assert k in [f.attname for f in self._meta.fields], \
|
2012-11-26 02:31:30 +08:00
|
|
|
"Author.__init__ got an unexpected parameter: %s" % k
|
2009-12-20 10:46:58 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-12-20 10:46:58 +08:00
|
|
|
class Book(models.Model):
|
|
|
|
title = models.CharField(max_length=255)
|
2015-07-22 22:43:21 +08:00
|
|
|
author = models.ForeignKey(Author, models.CASCADE)
|
2013-08-12 04:19:09 +08:00
|
|
|
paperback = models.BooleanField(default=False)
|
2010-04-02 00:48:16 +08:00
|
|
|
opening_line = models.TextField()
|
2009-12-20 10:46:58 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2015-07-23 02:54:42 +08:00
|
|
|
class BookFkAsPk(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
book = models.ForeignKey(Book, models.CASCADE, primary_key=True, db_column="not_the_default")
|
2015-07-23 02:54:42 +08:00
|
|
|
|
|
|
|
|
2009-12-20 10:46:58 +08:00
|
|
|
class Coffee(models.Model):
|
|
|
|
brand = models.CharField(max_length=255, db_column="name")
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-12-20 10:46:58 +08:00
|
|
|
class Reviewer(models.Model):
|
2010-02-24 03:39:06 +08:00
|
|
|
reviewed = models.ManyToManyField(Book)
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2010-02-24 03:39:06 +08:00
|
|
|
class FriendlyAuthor(Author):
|
|
|
|
pass
|