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-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
|
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
|
|
|
|
2009-04-03 19:52:14 +00:00
|
|
|
class Link(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2009-04-03 19:52:14 +00:00
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 06:43:33 +00:00
|
|
|
content_object = GenericForeignKey()
|
2009-04-03 19:52:14 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2017-07-21 22:21:13 +00:00
|
|
|
class LinkProxy(Link):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
|
2009-04-03 19:52:14 +00:00
|
|
|
class Place(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2018-03-13 02:42:48 +00:00
|
|
|
links = GenericRelation(Link, related_query_name='places')
|
2017-07-21 22:21:13 +00:00
|
|
|
link_proxy = GenericRelation(LinkProxy)
|
2010-01-22 18:39:48 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-01-22 18:39:48 +00:00
|
|
|
class Restaurant(Place):
|
2020-04-30 07:13:23 +00:00
|
|
|
pass
|
2010-01-22 18:39:48 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2017-12-05 21:08:50 +00:00
|
|
|
class Cafe(Restaurant):
|
2020-04-30 07:13:23 +00:00
|
|
|
pass
|
2017-12-05 21:08:50 +00:00
|
|
|
|
|
|
|
|
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)
|
2015-07-22 14:43:21 +00:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2010-01-22 18:39:48 +00:00
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 06:43:33 +00:00
|
|
|
content_object = GenericForeignKey()
|
2010-01-22 18:39:48 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
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
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-01-29 17:58:22 +00:00
|
|
|
class CharLink(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2010-01-29 17:58:22 +00:00
|
|
|
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):
|
2015-07-22 14:43:21 +00:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2010-01-29 17:58:22 +00:00
|
|
|
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):
|
2015-07-22 14:43:21 +00:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2010-02-10 19:21:52 +00:00
|
|
|
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
|
|
|
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
|
|
|
|
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
|
|
|
class Team(models.Model):
|
|
|
|
name = models.CharField(max_length=15)
|
|
|
|
members = models.ManyToManyField(Developer)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-01-19 14:55:03 +00:00
|
|
|
def __bool__(self):
|
|
|
|
return False
|
2013-02-06 02:35:10 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-02-06 02:35:10 +00:00
|
|
|
class Tag(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE, related_name='g_r_r_tags')
|
2013-02-06 02:35:10 +00:00
|
|
|
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):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2013-11-12 19:35:52 +00:00
|
|
|
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):
|
2018-08-09 01:25:18 +00:00
|
|
|
links = SpecialGenericRelation(Link, related_query_name='targets')
|
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):
|
2017-05-06 14:56:28 +00:00
|
|
|
flag = models.BooleanField(null=True)
|
2015-07-22 14:43:21 +00:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2013-06-05 21:29:44 +00:00
|
|
|
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):
|
2015-07-22 14:43:21 +00:00
|
|
|
b = models.ForeignKey(B, models.CASCADE)
|
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 D(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
b = models.ForeignKey(B, models.SET_NULL, null=True)
|
2013-06-05 21:29:44 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|
2014-07-14 16:42:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Ticket #22998
|
|
|
|
|
|
|
|
class Node(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2014-07-14 16:42:14 +00:00
|
|
|
object_id = models.PositiveIntegerField()
|
|
|
|
content = GenericForeignKey('content_type', 'object_id')
|
|
|
|
|
|
|
|
|
|
|
|
class Content(models.Model):
|
|
|
|
nodes = GenericRelation(Node)
|
2015-07-22 14:43:21 +00:00
|
|
|
related_obj = models.ForeignKey('Related', models.CASCADE)
|
2014-07-14 16:42:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Related(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def prevent_deletes(sender, instance, **kwargs):
|
2019-08-20 07:54:41 +00:00
|
|
|
raise models.ProtectedError("Not allowed to delete.", [instance])
|
2014-07-14 16:42:14 +00:00
|
|
|
|
2016-11-12 17:11:23 +00:00
|
|
|
|
2014-07-14 16:42:14 +00:00
|
|
|
models.signals.pre_delete.connect(prevent_deletes, sender=Node)
|