RobinReborn
RobinReborn

Reputation: 451

Tests to valid that an Django Object has other Objects with it as a foreign key

I'm writing a website that lets people create their own interactive quizzes.

I have three Objects: Quiz, Question and Answer. Question has a Quiz field (which uses Quiz as a foreign key), Answer has a Question field (which uses Question as a foreign key).

I'd like to make sure that a Quiz has at least one question and that a Question has at least two Answers. I've had trouble because in order to even add a Question to a Quiz, I must first save the quiz.

What can I do to assure that users don't see Quizzes without Questions or Questions without at least two Answers?

Upvotes: 0

Views: 31

Answers (1)

Darshan
Darshan

Reputation: 457

To prevent quiz from being saved ,you allow null values in foreign key and create quiz at the end when all the questions been created. same way you can do with questions and answer.

Upvotes: 1

Related Questions