简体   繁体   English

在Django中,对于同一类的两个对象之间的OneToOne关系,是否可以防止某个对象在数据库级别引用其自身?

[英]In Django, for a OneToOne relationship between two objects of the same class, can an object be prevented from referring to itself at the db level?

I have the following in my app: 我的应用程序中包含以下内容:

class University(models.Model):
    ...
    sister_university = models.OneToOneField('self', 
                        related_name = 
                        'university_sister_university', 
                        blank=True, null=True, 
                        on_delete=models.SET_NULL)

As it is, under the Django Admin site, I'm able to select University A as the sister_university of University A (itself). 实际上,在Django Admin站点下,我可以选择University A作为University A(本身)的sister_university。 Is it possible to enforce some sort of a rule at the database level so a university object can never be its own sister_university? 是否可以在数据库级别强制执行某种规则,以使大学对象永远不能成为其自己的sister_university?

Alternatively, is there a better way of accomplishing what I'm trying to do? 或者,是否有更好的方法来完成我要尝试的操作?

What you want is a check constraint, something like: 您想要的是一个check约束,例如:

alter table university
add constraint chk_non_self_ref check (id <> sister_university_id);

I'm not aware of a way to define this in Django models (but I'm guessing you can add it to a migration). 我不知道在Django模型中定义此方法的方法(但我猜您可以将其添加到迁移中)。

At the model level, you can override the clean() method of your model to implement that check every time a University instance is saved. 在模型级别,您可以覆盖模型的clean()方法,以在每次保存University实例时实施该检查。

def clean(self):

    if self.sister_university.id == self.id:
        raise ValidationError('A university object can never be its own sister_university.')

暂无
暂无

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

相关问题 我在Django应用程序中的同一个类的两个对象之间有一个OneToOne关系。 是否有可能强制实现这种关系的独特性? - I have a OneToOne relationship between two objects of the same class in a Django app. Is it possible to enforce the uniqueness of this relationship? 在Django中访问与模型具有OneToOne关系的对象 - Accessing objects with OneToOne relationship to a model in Django 来自同一 object 的 2 个不同引用到来自同一 class - DJANGO 数据库的两个不同对象 - 2 different references from the same object to two different objects from the same class - DJANGO database Django中的OneToMany和OneToOne关系之间的冲突 - conflict between OneToMany and OneToOne relationship in Django 在类本身中引用对象名称 - Referring to object names within its Class itself 如何在Django中不同模型的任何两个字段之间建立OneToOne关系? - How do i have OneToOne relationship between any two fields of different models in Django? 在同一视图中的2个模型(窗体)之间创建OneToOne关系 - Creating a OneToOne Relationship between 2 models(forms) in the same view 无法使用 Django Rest Framework 保存两个对象之间的关系 - Unable to save relationship between two objects with Django Rest Framework 无法使用OneToOne关系中的属性自动填充Django管理员字段 - Unable to autopopulate a Django admin field using an attribute from a OneToOne relationship Python:如何区分同一类的两个不同对象的两个变量? - Python: how can I differentiate between two variables of two different objects of the same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM