不要让用户创建多个具有相同值的 objects
don't let user create multiple objects with same value
我有一个带有名为“title”的外键的模型。我想限制每个用户只能为每个标题创建一次 object,这样他们就不能有多个具有相同标题的 object。有人知道我怎样才能做到这一点吗?
我试过在我的模型中添加这样的“unique_together”,但它不起作用。
class Meta:
unique_together = ('user', 'title')
您可以检查是否已经存在具有相同用户和标题的object:
if YourModel.objects.filter(user=..., title= ....):
.... here is the error handling ...
else:
.... save object
我有一个带有名为“title”的外键的模型。我想限制每个用户只能为每个标题创建一次 object,这样他们就不能有多个具有相同标题的 object。有人知道我怎样才能做到这一点吗?
我试过在我的模型中添加这样的“unique_together”,但它不起作用。
class Meta:
unique_together = ('user', 'title')
您可以检查是否已经存在具有相同用户和标题的object:
if YourModel.objects.filter(user=..., title= ....):
.... here is the error handling ...
else:
.... save object