0

I need to make sure the entry does not have the same project and case id.

How is the best way to prevent a duplicate entry?

Here the project can not have the same case twice.

class Cases(models.Model ): project = models.ForeignKey ( Project, on_delete = models.CASCADE ) case = models.ForeignKey ( Case, on_delete = models.CASCADE ) active = models.BooleanField ( default = 1 ) 

Thank you.

1

1 Answer 1

2

You can use unique_together in Meta class in you model as below.

class Cases(models.Model ): project = models.ForeignKey ( Project, on_delete = models.CASCADE ) case = models.ForeignKey ( Case, on_delete = models.CASCADE ) active = models.BooleanField ( default = 1 ) class Meta: unique_together = ('project', 'case') 

This will set unique constraint on project_id and case_id.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.