0

I have a model with a foreign key called "title". I want to restrict each user to only be able to create an object with each title once, so that they can't have multiple objects with the same title. Does anybody know how I can achieve this?

I have tried to add "unique_together" like this in my model but it does not work.

class Meta:
    unique_together = ('user', 'title')

1 Answer 1

1

You can check if an object with same user and title exists already:

if YourModel.objects.filter(user=..., title= ....):
     .... here is the error handling ...
else:
     .... save object
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.