feat: 增加safe_get_or_create方法
This commit is contained in:
parent
3045b46d77
commit
3ef3607220
|
@ -111,6 +111,14 @@ class BaseModel(models.Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def safe_get_or_create(cls, **kwargs):
|
||||||
|
defaults = kwargs.pop('defaults', {})
|
||||||
|
try:
|
||||||
|
return cls.objects.get_or_create(defaults=defaults, **kwargs)
|
||||||
|
except IntegrityError:
|
||||||
|
return cls.objects.get(**kwargs), False
|
||||||
|
|
||||||
def handle_parent(self):
|
def handle_parent(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue