You can create and edit choices right in the Question admin view. You can even control how many blank slots to show by default. Go look at ChoiceInline admin here in the tutorial: https://docs.djangoproject.com/en/dev/intro/tutorial02/
The next step would be to create an admin action on Question to duplicate the question and its choices. https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/ That way, your workflow is duplicate a question, edit on a single page, hit save. Brian Schott bfsch...@gmail.com On Apr 10, 2013, at 3:02 PM, Cody Scott <cody.j.b.sc...@gmail.com> wrote: > I like the is_correct and the count attributes! > > The problem with that it is hard to make a quiz in the admin section. You > create a quiz and a question but then you have to go to the choice section > and create choices for the question. And even if you have the same choice in > two questions you need to create the choice multiple times. > > It would be nice to have text fields and be able to say add a 5th choice, and > just enter text. It seems easier than changing forms and selecting an option > from a large list, or even in your case creating each choice for a quiz. > > Is there a way to have the admin link from Question to Choice in this > configuration? > > On Wednesday, 10 April 2013 14:43:54 UTC-4, BFSchott wrote: > Is a many-to-many what you really want? How common are the choices and > suppose someone goes in an edits one of the choices and suddenly it doesn't > match up right with a different linked question. It might make sense to use > a reverse foreign key one-to-many relationship and provide a mechanism to > duplicate a question, including its member choices. That way you could add > some statistics on each choice unique to a question. > > Several possible paths depending on how you want to go. > > 1. You could just use a reverse foreign key and have a one-to-many > relationship. > > class Choice(models.Model): > question = models.ForeignKey('Question', related_name='choices') > choice = models.CharField(max_length=255) > index = models.IntegerField(default=0) # in case you want order_by > filtering > count = models.IntegerField(default=0) # in case you want to count > number of times this is selected right or wrong > is_correct = models.BooleanField(default=False) # boolean flag replaces > "answer" > > class Question(models.Model): > question = models.CharField(max_length=64) > module = .... > > def times_correct(self): > return > self.choices.objects.filter(is_correct=True).aggregate(Sum('count')) > > def times_total(self): > return self.choices.objects.aggregate(Sum('count')) > > > Then in the admin, you create a table inline admin and just add unique > entries. > > > 2. Encode the thing as a JSON list and store in a JSONField > https://github.com/derek-schaefer/django-json-field > > 3. There are several flavors of ListField out there: > http://djangosnippets.org/snippets/1491/ > > > > Brian Schott > bfsc...@gmail.com > > > > On Apr 10, 2013, at 1:38 PM, Cody Scott <cody.j....@gmail.com> wrote: > >> I am trying to store questions in a database. >> I don't to set a fixed number of options for the question, some questions >> could have 4 or 2 or 5. >> >> Currently I am using a ManyToManyField to a table that just contains a >> CharField. >> This works but creating an option requires making another Choice object and >> selecting that, also when you want to select a Choice that has already been >> created you have to use that little box in the admin and it doesn't scale >> when you have hundreds of options. >> >> Even if I wanted to have 4 options every time what is the recommended way >> without having four CharFields? >> >> class Choice(models.Model): >> choice = models.CharField(max_length=255) >> def __unicode__(self): >> return self.choice >> >> #multiple choice question >> class Question(models.Model): >> question = models.CharField(max_length=64) >> answer = models.CharField(max_length=255) >> choices = models.ManyToManyField(Choice, related_name='questions', >> verbose_name='options') >> module = models.ForeignKey('Module', related_name='questions') >> >> times_correct = models.IntegerField(editable=False, default=0) >> times_total = models.IntegerField(editable=False, default=0) >> >> def _get_average(self): >> "Returns the average in percent" >> if self.times_total != 0: >> return (self.times_correct / float(self.times_total))*100 >> return 0.0 >> average = property(_get_average) >> >> def __unicode__(self): >> return self.question >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users...@googlegroups.com. >> To post to this group, send email to django...@googlegroups.com. >> Visit this group at http://groups.google.com/group/django-users?hl=en. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.