Looking for some implementation ideas here. I am trying to design a custom polling system for my school, to allow teachers to give students polls to take.
I 开发者_如何学Gohave a Poll model, Question model (with a foreignkey to Poll model), and Choice model (with a foreignkey to the Question model).
What I need to be able to do is allow whoever is adding questions to choose the type of choices that will be shown. For example, one question should be able to be multiple choice (displayed via a radio buttons), and another question should be able to be "Check all that apply."
What is the best way to allow the creator of the poll to determine how the choices are shown? Should I do a CharField() with choices, and deal with it manually in the view? That doesn't seem efficient.
Use a field in your model that has choices for the different ways people can choose (it doesn't have to be CharField, you could also use a SmallIntegerField and map numbers). Create different form classes for each way of choosing and decide in the view which one to apply based on the value of the "way of choosing"-field. It's a straight-forward way and not that much of a hassle if abstracted nicely.
精彩评论