Debian Codegraph @

tests/m2m_regress/models.py

raw
1 from django.contrib.auth import models as auth
2 from django.db import models
3
4
5 # No related name is needed here, since symmetrical relations are not
6 # explicitly reversible.
7 class SelfRefer(models.Model):
8 name = models.CharField(max_length=10)
9 references = models.ManyToManyField("self")
10 related = models.ManyToManyField("self")
11
12 def __str__(self):
13 return self.name
14
15
16 class Tag(models.Model):
17 name = models.CharField(max_length=10)
18
19 def __str__(self):
20 return self.name
21
22
23 # Regression for #11956 -- a many to many to the base class
24 class TagCollection(Tag):
25 tags = models.ManyToManyField(Tag, related_name="tag_collections")
26
27 def __str__(self):
28 return self.name
29
30
31 # A related_name is required on one of the ManyToManyField entries here because
32 # they are both addressable as reverse relations from Tag.
33 class Entry(models.Model):
34 name = models.CharField(max_length=10)
35 topics = models.ManyToManyField(Tag)
36 related = models.ManyToManyField(Tag, related_name="similar")
37
38 def __str__(self):
39 return self.name
40
41
42 # Two models both inheriting from a base model with a self-referential m2m field
43 class SelfReferChild(SelfRefer):
44 pass
45
46
47 class SelfReferChildSibling(SelfRefer):
48 pass
49
50
51 # Many-to-Many relation between models, where one of the PK's isn't an Autofield
52 class Line(models.Model):
53 name = models.CharField(max_length=100)
54
55 def __str__(self):
56 return self.name
57
58
59 class Worksheet(models.Model):
60 id = models.CharField(primary_key=True, max_length=100)
61 lines = models.ManyToManyField(Line, blank=True)
62
63
64 # Regression for #11226 -- A model with the same name that another one to
65 # which it has a m2m relation. This shouldn't cause a name clash between
66 # the automatically created m2m intermediary table FK field names when
67 # running migrate
68 class User(models.Model):
69 name = models.CharField(max_length=30)
70 friends = models.ManyToManyField(auth.User)
71
72
73 class BadModelWithSplit(models.Model):
74 name = models.CharField(max_length=1)
75
76 class Meta:
77 abstract = True
78
79 def split(self):
80 raise RuntimeError("split should not be called")
81
82
83 class RegressionModelSplit(BadModelWithSplit):
84 """
85 Model with a split method should not cause an error in add_lazy_relation
86 """
87
88 others = models.ManyToManyField("self")
89
90
91 # Regression for #24505 -- Two ManyToManyFields with the same "to" model
92 # and related_name set to '+'.
93 class Post(models.Model):
94 primary_lines = models.ManyToManyField(Line, related_name="+")
95 secondary_lines = models.ManyToManyField(Line, related_name="+")