Django Unique Together (with foreign keys)

后端 未结 6 824
独厮守ぢ
独厮守ぢ 2020-11-28 11:35

I have a situation where I want to use the Meta options of unique_together to enforce a certain rule, here\'s the intermediary model:



        
6条回答
  •  情深已故
    2020-11-28 12:15

    You can't.

    The unique_together clause is directly translated to the SQL unique index. And you can only set those on columns of a single table, not a combination of several tables.

    You can add validation for it yourself though, simply overwrite the validate_unique method and add this validation to it.

    Docs: http://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.validate_unique

提交回复
热议问题