2009-12-22 15:18:51 +00:00
|
|
|
from django.contrib.auth.models import User
|
2014-01-22 06:43:33 +00:00
|
|
|
from django.contrib.contenttypes.fields import (
|
2015-01-28 12:35:27 +00:00
|
|
|
GenericForeignKey, GenericRelation,
|
2014-01-22 06:43:33 +00:00
|
|
|
)
|
2009-12-22 15:18:51 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2010-01-22 14:30:06 +00:00
|
|
|
from django.db import models
|
2009-12-22 15:18:51 +00:00
|
|
|
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2009-12-22 15:18:51 +00:00
|
|
|
class Review(models.Model):
|
|
|
|
source = models.CharField(max_length=100)
|
2015-07-22 14:43:21 +00:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2009-12-22 15:18:51 +00:00
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 06:43:33 +00:00
|
|
|
content_object = GenericForeignKey()
|
2009-12-22 15:18:51 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('source',)
|
|
|
|
|
2018-12-28 00:34:14 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.source
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2009-12-22 15:18:51 +00:00
|
|
|
class PersonManager(models.Manager):
|
|
|
|
def get_by_natural_key(self, name):
|
|
|
|
return self.get(name=name)
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2009-12-22 15:18:51 +00:00
|
|
|
class Person(models.Model):
|
2020-04-07 08:54:19 +00:00
|
|
|
name = models.CharField(max_length=100, unique=True)
|
2009-12-22 15:18:51 +00:00
|
|
|
|
2018-12-28 00:34:14 +00:00
|
|
|
objects = PersonManager()
|
2009-12-22 15:18:51 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2018-12-28 00:34:14 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2010-10-28 12:57:04 +00:00
|
|
|
# This book manager doesn't do anything interesting; it just
|
|
|
|
# exists to strip out the 'extra_arg' argument to certain
|
|
|
|
# calls. This argument is used to establish that the BookManager
|
|
|
|
# is actually getting used when it should be.
|
|
|
|
class BookManager(models.Manager):
|
2017-02-01 16:41:56 +00:00
|
|
|
def create(self, *args, extra_arg=None, **kwargs):
|
2017-01-21 13:13:44 +00:00
|
|
|
return super().create(*args, **kwargs)
|
2010-10-28 12:57:04 +00:00
|
|
|
|
2017-02-01 16:41:56 +00:00
|
|
|
def get_or_create(self, *args, extra_arg=None, **kwargs):
|
2017-01-21 13:13:44 +00:00
|
|
|
return super().get_or_create(*args, **kwargs)
|
2010-10-28 12:57:04 +00:00
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2009-12-22 15:18:51 +00:00
|
|
|
class Book(models.Model):
|
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
published = models.DateField()
|
|
|
|
authors = models.ManyToManyField(Person)
|
2015-07-22 14:43:21 +00:00
|
|
|
editor = models.ForeignKey(Person, models.SET_NULL, null=True, related_name='edited')
|
2014-01-22 06:43:33 +00:00
|
|
|
reviews = GenericRelation(Review)
|
2010-01-22 14:30:06 +00:00
|
|
|
pages = models.IntegerField(default=100)
|
2009-12-22 15:18:51 +00:00
|
|
|
|
2018-12-28 00:34:14 +00:00
|
|
|
objects = BookManager()
|
2009-12-22 15:18:51 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('title',)
|
|
|
|
|
2018-12-28 00:34:14 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2010-05-11 13:06:03 +00:00
|
|
|
class Pet(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2015-07-22 14:43:21 +00:00
|
|
|
owner = models.ForeignKey(Person, models.CASCADE)
|
2010-05-11 13:06:03 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2013-11-02 22:50:35 +00:00
|
|
|
|
2009-12-22 15:18:51 +00:00
|
|
|
class UserProfile(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
user = models.OneToOneField(User, models.SET_NULL, null=True)
|
2009-12-22 15:18:51 +00:00
|
|
|
flavor = models.CharField(max_length=100)
|
|
|
|
|
2010-04-28 04:20:35 +00:00
|
|
|
class Meta:
|
|
|
|
ordering = ('flavor',)
|