Note that there are some explanatory texts on larger screens.

plurals
  1. PODJango How to build a formset for a Quiz?
    text
    copied!<p>I'm working on a quiz application that needs to display a quiz to be taken My models .py looks like this</p> <pre><code>from django.db import models from django.contrib.auth.models import User from django.contrib import admin ####################### #Quiz Structure Models# ####################### class Quiz(models.Model): name = models.CharField(max_length = 255) creation = models.DateField(auto_now_add=True) creator = models.ForeignKey(User) def __unicode__ (self): return self.name def possible(self): total = 0 for question in self.question_set.all(): question.save() total += question.value return total class Question(models.Model): question = models.CharField(max_length = 255) quiz = models.ForeignKey(Quiz) creator = models.ForeignKey(User) creation = models.DateField(auto_now_add = True) #objective = TODO: include standards linking in later versions value = models.IntegerField(default = 1) def __unicode__(self): return self.question class Answer(models.Model): answer = models.CharField(max_length = 255) question = models.ForeignKey(Question) is_correct = models.BooleanField(default = False) #Creator is tied to the quiz ########## #Attempts# ########## class QuizAttempt(models.Model): student = models.ForeignKey(User) quiz = models.ForeignKey(Quiz) date = models.DateField(auto_now_add = True) #Score Method (similar to possible in Quiz class QuestionAttempt(models.Model): attempt = models.ForeignKey(QuizAttempt) question = models.ForeignKey(Question) response = models.ForeignKey(Answer) ####### #Admin# ####### class QuestionInline(admin.StackedInline): model = Question extra = 2 class AnswerInline(admin.StackedInline): model = Answer extra = 2 class QuizAdmin(admin.ModelAdmin): list_display = ('name', 'creator', 'creation', 'possible',) search_fields = ('name', 'creator') inlines = [QuestionInline] admin.site.register(Quiz, QuizAdmin) class QuestionAdmin(admin.ModelAdmin): inlines = [AnswerInline] search_fields = ('question', 'quiz', 'value',) list_display = ('question', 'quiz', 'value',) admin.site.register(Question, QuestionAdmin) </code></pre> <p>I'm trying to build a form or form-set that will let me have a form for a quiz attempt that contains all of the questions so it looks something like this, and will let me get that back in a view to save a student's attempt at a quiz.</p> <ol> <li>Question 1 <ul> <li>Option a</li> <li>ect</li> </ul></li> </ol> <p>So far it looks like my best solution is to build a formset out of these models, but I'm not sure how to specify my choices to limit it to answers associated with the current question, or how to get the right formset in a view.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload