2014-01-22 06:43:33 +00:00
|
|
|
from django.contrib.contenttypes.fields import (
|
|
|
|
GenericForeignKey, GenericRelation
|
|
|
|
)
|
2009-04-03 19:52:14 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2011-10-13 21:34:56 +00:00
|
|
|
from django.db import models
|
2012-08-12 10:32:08 +00:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2009-04-03 19:52:14 +00:00
|
|
|
|
2011-10-13 21:34:56 +00:00
|
|
|
__all__ = ('Link', 'Place', 'Restaurant', 'Person', 'Address',
|
|
|
|
'CharLink', 'TextLink', 'OddRelation1', 'OddRelation2',
|
2013-02-02 01:10:16 +00:00
|
|
|
'Contact', 'Organization', 'Note', 'Company')
|
2010-02-10 19:21:52 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2009-04-03 19:52:14 +00:00
|
|
|
class Link(models.Model):
|
|
|
|
content_type = models.ForeignKey(ContentType)
|
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 06:43:33 +00:00
|
|
|
content_object = GenericForeignKey()
|
2009-04-03 19:52:14 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2009-04-03 19:52:14 +00:00
|
|
|
return "Link to %s id=%s" % (self.content_type, self.object_id)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2009-04-03 19:52:14 +00:00
|
|
|
class Place(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2014-01-22 06:43:33 +00:00
|
|
|
links = GenericRelation(Link)
|
2010-01-22 18:39:48 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2009-04-03 19:52:14 +00:00
|
|
|
return "Place: %s" % self.name
|
2010-01-22 18:39:48 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2010-01-22 18:39:48 +00:00
|
|
|
class Restaurant(Place):
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2010-01-22 18:39:48 +00:00
|
|
|
return "Restaurant: %s" % self.name
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2010-01-22 18:39:48 +00:00
|
|
|
class Address(models.Model):
|
|
|
|
street = models.CharField(max_length=80)
|
|
|
|
city = models.CharField(max_length=50)
|
|
|
|
state = models.CharField(max_length=2)
|
|
|
|
zipcode = models.CharField(max_length=5)
|
|
|
|
content_type = models.ForeignKey(ContentType)
|
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 06:43:33 +00:00
|
|
|
content_object = GenericForeignKey()
|
2010-01-22 18:39:48 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2010-01-22 18:39:48 +00:00
|
|
|
return '%s %s, %s %s' % (self.street, self.city, self.state, self.zipcode)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2010-01-22 18:39:48 +00:00
|
|
|
class Person(models.Model):
|
|
|
|
account = models.IntegerField(primary_key=True)
|
|
|
|
name = models.CharField(max_length=128)
|
2014-01-22 06:43:33 +00:00
|
|
|
addresses = GenericRelation(Address)
|
2010-01-22 18:39:48 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2010-01-22 18:39:48 +00:00
|
|
|
return self.name
|
2010-01-29 17:58:22 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-01-29 17:58:22 +00:00
|
|
|
class CharLink(models.Model):
|
|
|
|
content_type = models.ForeignKey(ContentType)
|
|
|
|
object_id = models.CharField(max_length=100)
|
2014-01-22 06:43:33 +00:00
|
|
|
content_object = GenericForeignKey()
|
2010-01-29 17:58:22 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-01-29 17:58:22 +00:00
|
|
|
class TextLink(models.Model):
|
|
|
|
content_type = models.ForeignKey(ContentType)
|
|
|
|
object_id = models.TextField()
|
2014-01-22 06:43:33 +00:00
|
|
|
content_object = GenericForeignKey()
|
2010-01-29 17:58:22 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-01-29 17:58:22 +00:00
|
|
|
class OddRelation1(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2014-01-22 06:43:33 +00:00
|
|
|
clinks = GenericRelation(CharLink)
|
2010-01-29 17:58:22 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-01-29 17:58:22 +00:00
|
|
|
class OddRelation2(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2014-01-22 06:43:33 +00:00
|
|
|
tlinks = GenericRelation(TextLink)
|
2010-01-29 17:58:22 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-02-10 19:21:52 +00:00
|
|
|
# models for test_q_object_or:
|
|
|
|
class Note(models.Model):
|
|
|
|
content_type = models.ForeignKey(ContentType)
|
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 06:43:33 +00:00
|
|
|
content_object = GenericForeignKey()
|
2010-02-10 19:21:52 +00:00
|
|
|
note = models.TextField()
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-02-10 19:21:52 +00:00
|
|
|
class Contact(models.Model):
|
2014-01-22 06:43:33 +00:00
|
|
|
notes = GenericRelation(Note)
|
2010-02-10 19:21:52 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-02-10 19:21:52 +00:00
|
|
|
class Organization(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
contacts = models.ManyToManyField(Contact, related_name='organizations')
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-02-02 01:10:16 +00:00
|
|
|
@python_2_unicode_compatible
|
|
|
|
class Company(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2014-01-22 06:43:33 +00:00
|
|
|
links = GenericRelation(Link)
|
2013-02-02 01:10:16 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Company: %s" % self.name
|
2013-02-06 02:35:10 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-02-06 02:35:10 +00:00
|
|
|
# For testing #13085 fix, we also use Note model defined above
|
|
|
|
class Developer(models.Model):
|
|
|
|
name = models.CharField(max_length=15)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-02-06 02:35:10 +00:00
|
|
|
@python_2_unicode_compatible
|
|
|
|
class Team(models.Model):
|
|
|
|
name = models.CharField(max_length=15)
|
|
|
|
members = models.ManyToManyField(Developer)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s team" % self.name
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self.members.count()
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-02-06 02:35:10 +00:00
|
|
|
class Guild(models.Model):
|
|
|
|
name = models.CharField(max_length=15)
|
|
|
|
members = models.ManyToManyField(Developer)
|
|
|
|
|
|
|
|
def __nonzero__(self):
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-02-06 02:35:10 +00:00
|
|
|
return self.members.count()
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-02-06 02:35:10 +00:00
|
|
|
class Tag(models.Model):
|
|
|
|
content_type = models.ForeignKey(ContentType, related_name='g_r_r_tags')
|
|
|
|
object_id = models.CharField(max_length=15)
|
2014-01-22 06:43:33 +00:00
|
|
|
content_object = GenericForeignKey()
|
2013-02-06 02:35:10 +00:00
|
|
|
label = models.CharField(max_length=15)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-02-06 02:35:10 +00:00
|
|
|
class Board(models.Model):
|
|
|
|
name = models.CharField(primary_key=True, max_length=15)
|
2013-05-20 14:40:34 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2014-01-22 06:43:33 +00:00
|
|
|
class SpecialGenericRelation(GenericRelation):
|
2013-11-12 19:35:52 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(SpecialGenericRelation, self).__init__(*args, **kwargs)
|
|
|
|
self.editable = True
|
|
|
|
self.save_form_data_calls = 0
|
|
|
|
|
|
|
|
def save_form_data(self, *args, **kwargs):
|
|
|
|
self.save_form_data_calls += 1
|
|
|
|
|
|
|
|
|
2013-05-20 14:40:34 +00:00
|
|
|
class HasLinks(models.Model):
|
2013-11-12 19:35:52 +00:00
|
|
|
links = SpecialGenericRelation(Link)
|
2013-05-20 14:40:34 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-05-20 14:40:34 +00:00
|
|
|
class HasLinkThing(HasLinks):
|
|
|
|
pass
|
2013-06-05 21:29:44 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-06-05 21:29:44 +00:00
|
|
|
class A(models.Model):
|
|
|
|
flag = models.NullBooleanField()
|
|
|
|
content_type = models.ForeignKey(ContentType)
|
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 06:43:33 +00:00
|
|
|
content_object = GenericForeignKey('content_type', 'object_id')
|
2013-06-05 21:29:44 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-06-05 21:29:44 +00:00
|
|
|
class B(models.Model):
|
2014-01-22 06:43:33 +00:00
|
|
|
a = GenericRelation(A)
|
2013-06-05 21:29:44 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-06-05 21:29:44 +00:00
|
|
|
class C(models.Model):
|
|
|
|
b = models.ForeignKey(B)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-06-05 21:29:44 +00:00
|
|
|
class D(models.Model):
|
|
|
|
b = models.ForeignKey(B, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|