ltbesh
ltbesh

Reputation: 667

Django CreateView modelform

I am new to Django CBV and I am trying to use it correctly.

I want to enable the user to create a quizz, the user choose a subject, a level and a subject and based and those choices I draw 10 questions from the database and create the quizz with it. So the user is presented with a form containing 3 choicefield but in fact the model contains much more fields.

I use a ModelForm based on quizz and exclude some of the fields. The problem is that I don't know how to draw the questions once the user choose a subject a level and a chapter and save them afterward.

Here is the quizz model :

class Quizz(models.Model):

    user = models.ForeignKey(User)
    date_started = models.DateTimeField('date started')
    questions = models.ManyToManyField(Question, through = 'QuestionStatus')
    level = models.ForeignKey(Level, null = True)
    subject = models.ForeignKey(Subject, null = True)
    chapter = models.ForeignKey(Chapter, null = True)
    grade = models.IntegerField(default = 0)
    finished = models.BooleanField(default = False)

The form :

class QuizzCreateForm(forms.ModelForm):
    class Meta:
        model = Quizz
        fields = ('subject', 'level', 'chapter')

And the view :

class QuizzCreate(CreateView):
    model = Quizz        
    form_class = QuizzCreateForm

Thank you for your help

Upvotes: 1

Views: 1180

Answers (1)

ltbesh
ltbesh

Reputation: 667

I found that one of the way to go was to put my business logic in the form_valid form of the view, it works as I want.

Upvotes: 2

Related Questions