简体   繁体   English

来自抽象模型领域的Django unique_together

[英]Django unique_together from abstract model field

Suppose I have an Abstract Model: 假设我有一个抽象模型:

class SoftDelete(models.Model):
    _active = models.NullBooleanField(default=True)

class Meta:
    abstract = True

And a model that inerhits from this abstract model: 从这个抽象模型中得出的模型:

class SomeModel(AbstractModel):
    some_field = models.IntegerField()

class Meta:
    unique_together = ('_active', 'some_field')

Here I've constrained some_field with the _active field by using unique_together, which is used by a soft-delete feature I have. 在这里,我已经做了约束some_field_active使用unique_together,用于通过软删除功能我有场。

That works and all but, every model I have an unique constraint now needs to apply _active into the uniqueness since when deleted it's not really deleted, only _active = None . 那行得通,但是,所有具有唯一约束的模型现在都需要将_active应用于唯一性,因为删除后并不会真正删除它,只有_active = None

My question is , since all my models will inerhit from SoftDelete is there an efficient way of applying _active to all models that have in their Metas an unique_together constraint? 我的问题是 ,由于我的所有模型都会受到SoftDelete的SoftDelete是否有一种有效的方法将_active应用于其_active中具有unique_together约束的所有模型? Instead of manually adding it and maybe forget of it. 而不是手动添加它,也许会忘记它。

I was hoping to add something like bellow to abstract's Meta class: 我希望在抽象的Meta类中添加类似波纹管的内容:

For unique_together in child_unique_together:
    unique_together.append('_active')

回答我自己的问题:由于找不到更好的解决方案,因此我采用了艰苦的方法并逐个模型地实现了唯一的共同模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM