2009-12-22 23:18:51 +08:00
|
|
|
from django.contrib.auth.models import User
|
2014-01-22 14:43:33 +08:00
|
|
|
from django.contrib.contenttypes.fields import (
|
2015-01-28 20:35:27 +08:00
|
|
|
GenericForeignKey, GenericRelation,
|
2014-01-22 14:43:33 +08:00
|
|
|
)
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2010-01-22 22:30:06 +08:00
|
|
|
from django.db import models
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-12-22 23:18:51 +08:00
|
|
|
class Review(models.Model):
|
|
|
|
source = models.CharField(max_length=100)
|
|
|
|
content_type = models.ForeignKey(ContentType)
|
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 14:43:33 +08:00
|
|
|
content_object = GenericForeignKey()
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-12-22 23:18:51 +08:00
|
|
|
return self.source
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('source',)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
class PersonManager(models.Manager):
|
|
|
|
def get_by_natural_key(self, name):
|
|
|
|
return self.get(name=name)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-12-22 23:18:51 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
objects = PersonManager()
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-12-22 23:18:51 +08:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2010-10-28 20:57:04 +08: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):
|
|
|
|
def create(self, *args, **kwargs):
|
|
|
|
kwargs.pop('extra_arg', None)
|
|
|
|
return super(BookManager, self).create(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_or_create(self, *args, **kwargs):
|
|
|
|
kwargs.pop('extra_arg', None)
|
|
|
|
return super(BookManager, self).get_or_create(*args, **kwargs)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-12-22 23:18:51 +08:00
|
|
|
class Book(models.Model):
|
2010-10-28 20:57:04 +08:00
|
|
|
objects = BookManager()
|
2009-12-22 23:18:51 +08:00
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
published = models.DateField()
|
|
|
|
authors = models.ManyToManyField(Person)
|
|
|
|
editor = models.ForeignKey(Person, null=True, related_name='edited')
|
2014-01-22 14:43:33 +08:00
|
|
|
reviews = GenericRelation(Review)
|
2010-01-22 22:30:06 +08:00
|
|
|
pages = models.IntegerField(default=100)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-12-22 23:18:51 +08:00
|
|
|
return self.title
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('title',)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2010-05-11 21:06:03 +08:00
|
|
|
class Pet(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
owner = models.ForeignKey(Person)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2010-05-11 21:06:03 +08:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
class UserProfile(models.Model):
|
2010-04-28 12:20:35 +08:00
|
|
|
user = models.OneToOneField(User, null=True)
|
2009-12-22 23:18:51 +08:00
|
|
|
flavor = models.CharField(max_length=100)
|
|
|
|
|
2010-04-28 12:20:35 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ('flavor',)
|