在Django 2.2中,您可以摆脱 unique_together
,而推荐使用带有constraints
类的 UniqueConstraint
列表,可以很方便,因为它有一个 condition
arg。
MyObject 有三个外键,一个是可选的。
class MyObject(ModelBase):
parent=... # mandatory
source=... # mandatory
subsource=... # optional
class Meta:
constraints = [
models.UniqueConstraint(
fields=["parent", "source", "subsource"],
name="unique_subsource"
),
models.UniqueConstraint(
fields=["parent", "source"],
condition=models.Q(subsource=None),
name="unique_source",
),
]
想象一下,我尝试依次创建以下对象和预期的验证:
parent source subsource valid?
1. 1. 1. yes
1. 1. 2. yes
1. 1. - yes
1. 1. - no
1. 1. 3. yes
1. 2. - yes
...
所以我写了两个测试:
def test_unique1(self):
""" unique parent/source/subsource """
parent = ParentFactory()
source = SourceFactory()
subsource = SubsourceFactory()
MyObjectFactory(parent=parent, source=source, subsource=subsource)
myobj = MyObjectFactory.build(parent=parent, source=source, subsource=subsource)
self.should_raise_validation_error(myobj)
def test_unique2(self):
""" unique parent/source """
parent = ParentFactory()
source = SourceFactory()
subsource = SubsourceFactory()
MyObjectFactory(parent=parent, source=source)
myobj = MyObjectFactory.build(parent=parent, source=source)
self.should_raise_validation_error(myobj)
但是后者不会引发任何验证错误:
.....F.
======================================================================
FAIL: test_unique2
unique parent/source/subsource
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/sebastiennicolaidis/dev/python/scientcap/macrovars/tests/models/test_dsflink.py", line 50, in test_unique2
self.should_raise_validation_error(dsflink)
File "/Users/sebastiennicolaidis/dev/python/scientcap/shared/tests/base_model_test.py", line 9, in should_raise_validation_error
instance.full_clean()
AssertionError: ValidationError not raised
----------------------------------------------------------------------
Ran 7 tests in 0.141s
BEGIN;
--
-- Change Meta options on dataset
--
--
-- Change Meta options on dsforecast
--
--
-- Alter unique_together for dsflink (0 constraint(s))
--
ALTER TABLE "macrovars_dsflink" DROP CONSTRAINT "macrovars_dsflink_parent_id_source_id_73901780_uniq";
--
-- Create constraint unique_subsource on model dsflink
--
CREATE UNIQUE INDEX "unique_subsource" ON "macrovars_dsflink" ("parent_id", "source_id", "subsource_id") WHERE "subsource_id" IS NOT NULL;
--
-- Create constraint unique_source on model dsflink
--
CREATE UNIQUE INDEX "unique_source" ON "macrovars_dsflink" ("parent_id", "source_id") WHERE "subsource_id" IS NULL;
COMMIT;
答案 0 :(得分:2)
我认为解决方案是同时创建带有条件的约束。
class MyObject(ModelBase):
parent=... # mandatory
source=... # mandatory
subsource=... # optional
class Meta:
constraints = [
models.UniqueConstraint(
fields=["parent", "source", "subsource"],
condition=models.Q(subsource__isnull=False), # <- filter subsource is null
name="unique_subsource"
),
models.UniqueConstraint(
fields=["parent", "source"],
condition=models.Q(subsource__isnull=True),
name="unique_source",
),
]