我有一个外键叫“标题”的模型。我希望限制每个用户只能创建一个带有每个标题的对象一次,这样他们就不能有多个具有相同标题的对象。有谁知道我是怎么做到的吗?
我尝试在我的模型中添加像这样的"unique_together“,但是它不起作用。
class Meta:
unique_together = ('user', 'title')
发布于 2022-01-31 13:33:53
您可以检查具有相同用户和标题的对象是否已经存在:
if YourModel.objects.filter(user=..., title= ....):
.... here is the error handling ...
else:
.... save object
https://stackoverflow.com/questions/70933021
复制相似问题