| 1 |
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation |
| 2 |
from django.contrib.contenttypes.models import ContentType |
| 3 |
from django.db import models |
| 4 |
|
| 5 |
__all__ = ( |
| 6 |
"Link", |
| 7 |
"Place", |
| 8 |
"Restaurant", |
| 9 |
"Person", |
| 10 |
"Address", |
| 11 |
"CharLink", |
| 12 |
"TextLink", |
| 13 |
"OddRelation1", |
| 14 |
"OddRelation2", |
| 15 |
"Contact", |
| 16 |
"Organization", |
| 17 |
"Note", |
| 18 |
"Company", |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
class Link(models.Model): |
| 23 |
content_type = models.ForeignKey(ContentType, models.CASCADE) |
| 24 |
object_id = models.PositiveIntegerField() |
| 25 |
content_object = GenericForeignKey() |
| 26 |
|
| 27 |
|
| 28 |
class LinkProxy(Link): |
| 29 |
class Meta: |
| 30 |
proxy = True |
| 31 |
|
| 32 |
|
| 33 |
class Place(models.Model): |
| 34 |
name = models.CharField(max_length=100) |
| 35 |
links = GenericRelation(Link, related_query_name="places") |
| 36 |
link_proxy = GenericRelation(LinkProxy) |
| 37 |
|
| 38 |
|
| 39 |
class Restaurant(Place): |
| 40 |
pass |
| 41 |
|
| 42 |
|
| 43 |
class Cafe(Restaurant): |
| 44 |
pass |
| 45 |
|
| 46 |
|
| 47 |
class Address(models.Model): |
| 48 |
street = models.CharField(max_length=80) |
| 49 |
city = models.CharField(max_length=50) |
| 50 |
state = models.CharField(max_length=2) |
| 51 |
zipcode = models.CharField(max_length=5) |
| 52 |
content_type = models.ForeignKey(ContentType, models.CASCADE) |
| 53 |
object_id = models.PositiveIntegerField() |
| 54 |
content_object = GenericForeignKey() |
| 55 |
|
| 56 |
|
| 57 |
class Person(models.Model): |
| 58 |
account = models.IntegerField(primary_key=True) |
| 59 |
name = models.CharField(max_length=128) |
| 60 |
addresses = GenericRelation(Address) |
| 61 |
|
| 62 |
|
| 63 |
class CharLink(models.Model): |
| 64 |
content_type = models.ForeignKey(ContentType, models.CASCADE) |
| 65 |
object_id = models.CharField(max_length=100) |
| 66 |
content_object = GenericForeignKey() |
| 67 |
value = models.CharField(max_length=250) |
| 68 |
|
| 69 |
|
| 70 |
class TextLink(models.Model): |
| 71 |
content_type = models.ForeignKey(ContentType, models.CASCADE) |
| 72 |
object_id = models.TextField() |
| 73 |
content_object = GenericForeignKey() |
| 74 |
value = models.CharField(max_length=250) |
| 75 |
|
| 76 |
|
| 77 |
class OddRelation1(models.Model): |
| 78 |
name = models.CharField(max_length=100) |
| 79 |
clinks = GenericRelation(CharLink) |
| 80 |
|
| 81 |
|
| 82 |
class OddRelation2(models.Model): |
| 83 |
name = models.CharField(max_length=100) |
| 84 |
tlinks = GenericRelation(TextLink) |
| 85 |
|
| 86 |
|
| 87 |
# models for test_q_object_or: |
| 88 |
class Note(models.Model): |
| 89 |
content_type = models.ForeignKey(ContentType, models.CASCADE) |
| 90 |
object_id = models.PositiveIntegerField() |
| 91 |
content_object = GenericForeignKey() |
| 92 |
note = models.TextField() |
| 93 |
|
| 94 |
|
| 95 |
class Contact(models.Model): |
| 96 |
notes = GenericRelation(Note) |
| 97 |
|
| 98 |
|
| 99 |
class Organization(models.Model): |
| 100 |
name = models.CharField(max_length=255) |
| 101 |
contacts = models.ManyToManyField(Contact, related_name="organizations") |
| 102 |
|
| 103 |
|
| 104 |
class Company(models.Model): |
| 105 |
name = models.CharField(max_length=100) |
| 106 |
links = GenericRelation(Link) |
| 107 |
|
| 108 |
|
| 109 |
class Team(models.Model): |
| 110 |
name = models.CharField(max_length=15) |
| 111 |
|
| 112 |
def __len__(self): |
| 113 |
return 0 |
| 114 |
|
| 115 |
|
| 116 |
class Guild(models.Model): |
| 117 |
name = models.CharField(max_length=15) |
| 118 |
|
| 119 |
def __bool__(self): |
| 120 |
return False |
| 121 |
|
| 122 |
|
| 123 |
class Tag(models.Model): |
| 124 |
content_type = models.ForeignKey( |
| 125 |
ContentType, models.CASCADE, related_name="g_r_r_tags" |
| 126 |
) |
| 127 |
object_id = models.CharField(max_length=15) |
| 128 |
content_object = GenericForeignKey() |
| 129 |
label = models.CharField(max_length=15) |
| 130 |
|
| 131 |
|
| 132 |
class Board(models.Model): |
| 133 |
name = models.CharField(primary_key=True, max_length=25) |
| 134 |
|
| 135 |
|
| 136 |
class SpecialGenericRelation(GenericRelation): |
| 137 |
def __init__(self, *args, **kwargs): |
| 138 |
super().__init__(*args, **kwargs) |
| 139 |
self.editable = True |
| 140 |
self.save_form_data_calls = 0 |
| 141 |
|
| 142 |
def save_form_data(self, *args, **kwargs): |
| 143 |
self.save_form_data_calls += 1 |
| 144 |
|
| 145 |
|
| 146 |
class HasLinks(models.Model): |
| 147 |
links = SpecialGenericRelation(Link, related_query_name="targets") |
| 148 |
|
| 149 |
class Meta: |
| 150 |
abstract = True |
| 151 |
|
| 152 |
|
| 153 |
class HasLinkThing(HasLinks): |
| 154 |
pass |
| 155 |
|
| 156 |
|
| 157 |
class A(models.Model): |
| 158 |
flag = models.BooleanField(null=True) |
| 159 |
content_type = models.ForeignKey(ContentType, models.CASCADE) |
| 160 |
object_id = models.PositiveIntegerField() |
| 161 |
content_object = GenericForeignKey("content_type", "object_id") |
| 162 |
|
| 163 |
|
| 164 |
class B(models.Model): |
| 165 |
a = GenericRelation(A) |
| 166 |
|
| 167 |
class Meta: |
| 168 |
ordering = ("id",) |
| 169 |
|
| 170 |
|
| 171 |
class C(models.Model): |
| 172 |
b = models.ForeignKey(B, models.CASCADE) |
| 173 |
|
| 174 |
class Meta: |
| 175 |
ordering = ("id",) |
| 176 |
|
| 177 |
|
| 178 |
class D(models.Model): |
| 179 |
b = models.ForeignKey(B, models.SET_NULL, null=True) |
| 180 |
|
| 181 |
class Meta: |
| 182 |
ordering = ("id",) |
| 183 |
|
| 184 |
|
| 185 |
# Ticket #22998 |
| 186 |
|
| 187 |
|
| 188 |
class Node(models.Model): |
| 189 |
content_type = models.ForeignKey(ContentType, models.CASCADE) |
| 190 |
object_id = models.PositiveIntegerField() |
| 191 |
content = GenericForeignKey("content_type", "object_id") |
| 192 |
|
| 193 |
|
| 194 |
class Content(models.Model): |
| 195 |
nodes = GenericRelation(Node) |
| 196 |
related_obj = models.ForeignKey("Related", models.CASCADE) |
| 197 |
|
| 198 |
|
| 199 |
class Related(models.Model): |
| 200 |
pass |
| 201 |
|
| 202 |
|
| 203 |
def prevent_deletes(sender, instance, **kwargs): |
| 204 |
raise models.ProtectedError("Not allowed to delete.", [instance]) |
| 205 |
|
| 206 |
|
| 207 |
models.signals.pre_delete.connect(prevent_deletes, sender=Node) |