-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel_inheritance.py
74 lines (50 loc) · 1.98 KB
/
model_inheritance.py
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
from django.db import models
from django.db.models import TextChoices
from django.db.models.signals import pre_save
"""
Пример модели уведомлений для изменения статусов разных моделей.
"""
class NtType(TextChoices):
COUNTRY = 'COUNTRY', 'COUNTRY'
CITY = 'CITY', 'CITY'
SIMPLE = 'SIMPLE', 'SIMPLE'
class Nt(models.Model):
user = models.ForeignKey('common.User', models.CASCADE)
message = models.TextField('сообщение')
type = models.TextField('тип', choices=NtType.choices)
def build_message(self):
pass
def entity(self) -> models.Model:
if hasattr(self, 'nt_city'):
return self.nt_city.entity()
elif hasattr(self, 'nt_country'):
return self.nt_country.entity()
class NtCountry(Nt):
def __init__(self, *args, **kwargs):
self._meta.get_field('type').default = NtType.COUNTRY
super().__init__(*args, **kwargs)
country = models.ForeignKey('common.Country', models.CASCADE)
def build_message(self):
return f"Страна {self.country}, {self.user}"
def entity(self):
return self.country
class NtCity(Nt):
def __init__(self, *args, **kwargs):
self._meta.get_field('type').default = NtType.CITY
super().__init__(*args, **kwargs)
city = models.ForeignKey('common.City', models.CASCADE)
def build_message(self):
return f"Город {self.city} {self.city.region.country}"
def entity(self):
return self.city
class NtSimple(Nt):
def __init__(self, *args, **kwargs):
self._meta.get_field('type').default = NtType.SIMPLE
super().__init__(*args, **kwargs)
def build_message(self):
return f"NtSimple, {self.user}"
def nt_pre_save(sender, **kwargs):
instance: Nt = kwargs.get('instance')
if instance.pk is None and instance.message is None:
instance.message = instance.build_message()
pre_save.connect(nt_pre_save, sender=Nt)