I've got a Django web application that's like the typical Q&A system.

A person asks an issue. other customers submit solutions to that particular question:

  • Each user is permitted to submit as much as N solutions to every question, where N > 1 (so, say each user can submit a maximum of 3 solutions to each question)
  • A person can edit his existing solutions, or submit new answer(s) if he has not already arrived at his limit.

It's straightforward to get this done if each user is permitted only one answer - simply do:

unique_together = (("user.id", "question_id"),)

But just in case of N>1, what's the easiest method to implement this?

I'd add the next approach to your Question model:

class Question(models.Model):

    def can_answer(self, user):
        return Answer.filter(question=self, user=user).count() < 3

This can be used approach to determine if a person can also add solutions towards the question.

This can be a business rule that you may have to enforce in the application level as opposed to the database level. Hence you'll have to check during the time of responding to when the user can certainly publish a solution. This is often completed in multiple ways. Andrey's answer is an excellent method to do this. One other way is always to look for this throughout Answer.save(). If you'd rather keep your logic from models you are able to define an utility function can_answer(user, question) that returns True or False and invoke it in the view.