-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Expand file tree
/
Copy pathmodels.py
More file actions
170 lines (123 loc) · 5.43 KB
/
models.py
File metadata and controls
170 lines (123 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import uuid
from django.contrib.auth.models import User
from django.db import models
from django.db.models import QuerySet
from django.db.models.manager import BaseManager
from django.utils.translation import gettext_lazy as _
class RESTFrameworkModel(models.Model):
"""
Base for test models that sets app_label, so they play nicely.
"""
class Meta:
app_label = 'tests'
abstract = True
class BasicModel(RESTFrameworkModel):
text = models.CharField(
max_length=100,
verbose_name=_("Text comes here"),
help_text=_("Text description.")
)
# Models for relations tests
# ManyToMany
class ManyToManyTarget(RESTFrameworkModel):
name = models.CharField(max_length=100)
class ManyToManySource(RESTFrameworkModel):
name = models.CharField(max_length=100)
targets = models.ManyToManyField(ManyToManyTarget, related_name='sources')
class BasicModelWithUsers(RESTFrameworkModel):
users = models.ManyToManyField(User)
# ForeignKey
class ForeignKeyTarget(RESTFrameworkModel):
name = models.CharField(max_length=100)
def get_first_source(self):
"""Used for testing related field against a callable."""
return self.sources.all().order_by('pk')[0]
@property
def first_source(self):
"""Used for testing related field against a property."""
return self.sources.all().order_by('pk')[0]
class UUIDForeignKeyTarget(RESTFrameworkModel):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4)
name = models.CharField(max_length=100)
class ForeignKeySource(RESTFrameworkModel):
name = models.CharField(max_length=100)
target = models.ForeignKey(ForeignKeyTarget, related_name='sources',
help_text='Target', verbose_name='Target',
on_delete=models.CASCADE)
class ForeignKeySourceWithLimitedChoices(RESTFrameworkModel):
target = models.ForeignKey(ForeignKeyTarget, help_text='Target',
verbose_name='Target',
limit_choices_to={"name__startswith": "limited-"},
on_delete=models.CASCADE)
class ForeignKeySourceWithQLimitedChoices(RESTFrameworkModel):
target = models.ForeignKey(ForeignKeyTarget, help_text='Target',
verbose_name='Target',
limit_choices_to=models.Q(name__startswith="limited-"),
on_delete=models.CASCADE)
# Nullable ForeignKey
class NullableForeignKeySource(RESTFrameworkModel):
name = models.CharField(max_length=100)
target = models.ForeignKey(ForeignKeyTarget, null=True, blank=True,
related_name='nullable_sources',
verbose_name='Optional target object',
on_delete=models.CASCADE)
class NullableUUIDForeignKeySource(RESTFrameworkModel):
name = models.CharField(max_length=100)
target = models.ForeignKey(ForeignKeyTarget, null=True, blank=True,
related_name='nullable_sources',
verbose_name='Optional target object',
on_delete=models.CASCADE)
class NestedForeignKeySource(RESTFrameworkModel):
"""
Used for testing FK chain. A -> B -> C.
"""
name = models.CharField(max_length=100)
target = models.ForeignKey(NullableForeignKeySource, null=True, blank=True,
related_name='nested_sources',
verbose_name='Intermediate target object',
on_delete=models.CASCADE)
# OneToOne
class OneToOneTarget(RESTFrameworkModel):
name = models.CharField(max_length=100)
class NullableOneToOneSource(RESTFrameworkModel):
name = models.CharField(max_length=100)
target = models.OneToOneField(
OneToOneTarget, null=True, blank=True,
related_name='nullable_source', on_delete=models.CASCADE)
class OneToOnePKSource(RESTFrameworkModel):
""" Test model where the primary key is a OneToOneField with another model. """
name = models.CharField(max_length=100)
target = models.OneToOneField(
OneToOneTarget, primary_key=True,
related_name='required_source', on_delete=models.CASCADE)
class CustomManagerModel(RESTFrameworkModel):
class CustomManager:
def __new__(cls, *args, **kwargs):
cls = BaseManager.from_queryset(
QuerySet
)
return cls
objects = CustomManager()()
# `CustomManager()` will return a `BaseManager` class.
# We need to instantiation it, so we write `CustomManager()()` here.
text = models.CharField(
max_length=100,
verbose_name=_("Text comes here"),
help_text=_("Text description.")
)
o2o_target = models.ForeignKey(OneToOneTarget,
help_text='OneToOneTarget',
verbose_name='OneToOneTarget',
on_delete=models.CASCADE)
class ListModelForTest(RESTFrameworkModel):
name = models.CharField(max_length=100)
status = models.CharField(max_length=100, blank=True)
@property
def is_valid(self):
return self.name == 'valid'
class EmailPKModel(RESTFrameworkModel):
email = models.EmailField(primary_key=True)
name = models.CharField(max_length=100)
@property
def is_valid(self):
return self.name == 'valid'