2020-07-31 10:18:52 +00:00
|
|
|
|
"""
|
|
|
|
|
此文件作为 `django.db.models` 的 shortcut
|
|
|
|
|
|
|
|
|
|
这样做的优点与缺点为:
|
|
|
|
|
优点:
|
|
|
|
|
- 包命名都统一为 `models`
|
|
|
|
|
- 用户在使用的时候只导入本文件即可
|
|
|
|
|
缺点:
|
|
|
|
|
- 此文件中添加代码的时候,注意不要跟 `django.db.models` 中的命名冲突
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import uuid
|
2022-12-21 09:36:44 +00:00
|
|
|
|
from functools import reduce
|
2020-07-31 10:18:52 +00:00
|
|
|
|
|
2022-04-07 10:51:35 +00:00
|
|
|
|
from django.db import models
|
2022-06-23 05:52:28 +00:00
|
|
|
|
from django.db import transaction
|
2022-12-05 08:07:14 +00:00
|
|
|
|
from django.db.models import F, ExpressionWrapper, CASCADE
|
2021-02-25 06:45:21 +00:00
|
|
|
|
from django.db.models import QuerySet
|
2020-07-31 10:18:52 +00:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-07-20 02:42:22 +00:00
|
|
|
|
|
2022-11-04 06:22:38 +00:00
|
|
|
|
from ..const.signals import SKIP_SIGNAL
|
|
|
|
|
|
2020-07-20 02:42:22 +00:00
|
|
|
|
|
2022-08-22 03:47:45 +00:00
|
|
|
|
class ChoicesMixin:
|
|
|
|
|
_value2label_map_: dict
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_label(cls, value: (str, int)):
|
|
|
|
|
return cls._value2label_map_[value]
|
|
|
|
|
|
|
|
|
|
|
2022-04-07 10:51:35 +00:00
|
|
|
|
class BaseCreateUpdateModel(models.Model):
|
2022-12-20 12:23:42 +00:00
|
|
|
|
created_by = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Created by'))
|
|
|
|
|
updated_by = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Updated by'))
|
2022-04-07 10:51:35 +00:00
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date created'))
|
|
|
|
|
date_updated = models.DateTimeField(auto_now=True, verbose_name=_('Date updated'))
|
2022-12-20 12:23:42 +00:00
|
|
|
|
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
|
2020-07-31 10:18:52 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
|
|
2022-04-07 10:51:35 +00:00
|
|
|
|
class JMSBaseModel(BaseCreateUpdateModel):
|
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2020-07-31 10:18:52 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
abstract = True
|
2020-08-03 08:17:46 +00:00
|
|
|
|
|
2022-11-04 06:22:38 +00:00
|
|
|
|
def __str__(self):
|
|
|
|
|
return str(self.id)
|
|
|
|
|
|
2020-08-03 08:17:46 +00:00
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
|
def output_as_string(field_name):
|
2022-04-07 10:51:35 +00:00
|
|
|
|
return ExpressionWrapper(F(field_name), output_field=models.CharField())
|
2021-02-25 06:45:21 +00:00
|
|
|
|
|
|
|
|
|
|
2022-06-23 05:52:28 +00:00
|
|
|
|
class MultiTableChildQueryset(QuerySet):
|
|
|
|
|
|
|
|
|
|
def bulk_create(self, objs, batch_size=None):
|
|
|
|
|
assert batch_size is None or batch_size > 0
|
|
|
|
|
if not objs:
|
|
|
|
|
return objs
|
|
|
|
|
|
|
|
|
|
self._for_write = True
|
|
|
|
|
objs = list(objs)
|
|
|
|
|
parent_model = self.model._meta.pk.related_model
|
|
|
|
|
|
|
|
|
|
parent_objs = []
|
|
|
|
|
for obj in objs:
|
|
|
|
|
parent_values = {}
|
|
|
|
|
for field in [f for f in parent_model._meta.fields if hasattr(obj, f.name)]:
|
|
|
|
|
parent_values[field.name] = getattr(obj, field.name)
|
|
|
|
|
parent_objs.append(parent_model(**parent_values))
|
|
|
|
|
setattr(obj, self.model._meta.pk.attname, obj.id)
|
|
|
|
|
parent_model.objects.bulk_create(parent_objs, batch_size=batch_size)
|
|
|
|
|
|
|
|
|
|
with transaction.atomic(using=self.db, savepoint=False):
|
|
|
|
|
self._batched_insert(objs, self.model._meta.local_fields, batch_size)
|
|
|
|
|
|
|
|
|
|
return objs
|
2022-11-04 06:22:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def CASCADE_SIGNAL_SKIP(collector, field, sub_objs, using):
|
|
|
|
|
# 级联删除时,操作日志标记不保存,以免用户混淆
|
|
|
|
|
try:
|
|
|
|
|
for obj in sub_objs:
|
|
|
|
|
setattr(obj, SKIP_SIGNAL, True)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
CASCADE(collector, field, sub_objs, using)
|