Getting AttributeError: module 'django.db.models' has no attribute 'UniqueConstraint' in Django 1.11

Getting AttributeError: module 'django.db.models' has no attribute 'UniqueConstraint' in Django 1.11

我正在使用 Django 1.11 并且在尝试导入时 UniqueConstraint,我变得很糟糕

AttributeError: module 'django.db.models' has no attribute 'UniqueConstraint'

错误。不想降到unique_together,因为好像贬值了。那么,为什么我在最新版本的 Django 中会出现此错误?!

我正在按以下方式导入它:

from django.db import models

class CalendarAccount(models.Model):
    
    class Meta:
        managed = False
        db_table = 'my_table'
        constraints = [
            models.UniqueConstraint(...),
        ]

如果您使用的是 django 1.11,那还不存在。

如果您移动到 ​​2.2,请使用 UniqueConstraint,但到那时您将设置 unique_together 并且它尚未弃用,因此没有必要更改它。

Django 1.11 docs for unique_together

作为一个指针,通过查看右下角文档的可用版本,您可以了解您正在阅读的内容是否与您正在使用的 django 版本相关。

docs for UniqueConstraint 仅显示 2.2、3.0 和开发版,因此在 2.2 之前不可用。

无论如何,文档在这里; https://docs.djangoproject.com/en/1.11/ref/models/options/#unique-together