| 1 |
from django.db import models |
| 2 |
|
| 3 |
|
| 4 |
class Author(models.Model): |
| 5 |
first_name = models.CharField(max_length=255) |
| 6 |
last_name = models.CharField(max_length=255) |
| 7 |
dob = models.DateField() |
| 8 |
|
| 9 |
def __init__(self, *args, **kwargs): |
| 10 |
super().__init__(*args, **kwargs) |
| 11 |
# Protect against annotations being passed to __init__ -- |
| 12 |
# this'll make the test suite get angry if annotations aren't |
| 13 |
# treated differently than fields. |
| 14 |
for k in kwargs: |
| 15 |
assert k in [f.attname for f in self._meta.fields], ( |
| 16 |
"Author.__init__ got an unexpected parameter: %s" % k |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
class Book(models.Model): |
| 21 |
title = models.CharField(max_length=255) |
| 22 |
author = models.ForeignKey(Author, models.CASCADE) |
| 23 |
paperback = models.BooleanField(default=False) |
| 24 |
opening_line = models.TextField() |
| 25 |
|
| 26 |
|
| 27 |
class BookFkAsPk(models.Model): |
| 28 |
book = models.ForeignKey( |
| 29 |
Book, models.CASCADE, primary_key=True, db_column="not_the_default" |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
class Coffee(models.Model): |
| 34 |
brand = models.CharField(max_length=255, db_column="name") |
| 35 |
price = models.DecimalField(max_digits=10, decimal_places=2, default=0) |
| 36 |
|
| 37 |
|
| 38 |
class MixedCaseIDColumn(models.Model): |
| 39 |
id = models.AutoField(primary_key=True, db_column="MiXeD_CaSe_Id") |
| 40 |
|
| 41 |
|
| 42 |
class Reviewer(models.Model): |
| 43 |
reviewed = models.ManyToManyField(Book) |
| 44 |
|
| 45 |
|
| 46 |
class FriendlyAuthor(Author): |
| 47 |
pass |