[Answered ]-How to make primay or unique three field in django models?

1👍

Django want only one field as primary key.
The solution in your case is to use unique_together for your three fields:

class Answer(models.Model):
    exam      = models.ForeignKey(Exam, on_delete=models.CASCADE, default=None)
    user      = models.ForeignKey(User, on_delete=models.CASCADE)
    question  = models.ForeignKey(Question, on_delete=models.CASCADE)
    answer    = models.CharField(max_length=8)

    class Meta:
        unique_together = ['user', 'exam', 'question']

Documentation about unique_together option: https://docs.djangoproject.com/fr/4.1/ref/models/options/#unique-together

Leave a comment