4

I'm creating a quiz app for my project. Each quiz can have multiple questions and each question has multiple answers from which one is correct.

The problem is that Django doesn't allow OneToMany relationship so I have to use ForeignKey in models.

class Language_Quiz(models.Model):
    name = models.CharField(max_length=40)
    language = models.OneToOneField(sfl_models.Language)


class Question(models.Model):
    language_quiz = models.ForeignKey(Language_Quiz)
    text = models.TextField()

class Answer(models.Model):
    question = models.ForeignKey(Question, related_name='answers')
    text = models.TextField()
    correct = models.BooleanField()

What I want to do is - Django admin would show list of Questions assigned to current Quiz with ability to add or modify them so admin can add questions while creating a Quiz.

Could you give me a hint? It should be something with admin.py

Antoine Pinsard
  • 33,148
  • 8
  • 67
  • 87
Milano
  • 18,048
  • 37
  • 153
  • 353

1 Answers1

-1

ForeignKey is the right field type. A OneToMany is only a reverse ManyToOne relation (which is a foreign key).

You can use InlineModelAdmin to edit a Quiz and its Questions on the same form.

Editing Answers on the same page would involve nested inline formsets, which is, AFAIK, not part of Django. If you want to achieve this, you might be interested in the following questions:

Community
  • 1
  • 1
Antoine Pinsard
  • 33,148
  • 8
  • 67
  • 87
  • Unfortunately it doesn't probably support Django <= 1.8. I've posted a question about the error here: http://stackoverflow.com/questions/38291894/attributeerror-module-object-has-no-attribute-commit-on-success – Milano Jul 10 '16 at 12:32