Skip to content
This repository was archived by the owner on Jun 15, 2020. It is now read-only.
Andrew Chan edited this page Oct 19, 2017 · 10 revisions

Question Model

question_text

Question.question_text

The text that the question will display.

question_type

Question.question_type

String representation of the question type. Use the same string as the class name (i.e. the string type for Radio Buttons should be "Radiobutton", since the class name is Radiobutton, and Radiobutton.__name__ evaluates to "Radiobutton"). In the future, if we keep the specific question models, it may be a good idea to modify the constructors so they pass in class.__name__ to this value automatically.

options

Question.options

A string representation of a python list - The list of NJ, TX, FL should be stored as "['NJ', 'TX', 'FL']". For consistency, groups that need to store and read the options should use list.__str__ and ast.literal_eval to convert between the string representation and a python list. options will be None if there are no options for that type of question (i.e. Paragraph).

Remember that we opted not to make option its own model because it would complicate the code a bit, so we made it a simple string that can be turned into a python list quickly. It would be great if we could just store python lists in the database, but unfortunately this is not possible, hence the string. In the future, we may want to modify the constructors for question types that need options (like Checkbox and Radiobutton) to accept a list and automatically stringify this list for us.

Example conversion between python list and string:

from portal.models import Question, Checkbox
import ast

x = ["NY", "TX", "FL"]
q = Question(
    question_text="What state are you from?", 
    question_type=Checkbox.__name__,
    options=x.__str__,
    order_number=1,
    )
# converting from a string to a python list
y = ast.literal_eval(q.options)

order_number

Question.order_number

Where on the application form this question should appear, in relation to other questions. Should start at 1 and all entries should be unique. The question that has number 1 would appear first, the one that has number 2 would be second and so on.

Models that inherit from Question (i.e. Paragraph, Checkbox, etc.)

  • To select all of the questions use Question.objects.all()
  • This will return all of the questions, but you can't use instanceof or look at the class name (they will only be Question type because of the way you looked them up). The only way to distinguish these is by looking at the question.question_type string.
  • Another way to do it would be selecting all of the different types of questions (i.e. checkboxes = Checkbox.objects.all(), paragraphs = Paragraphs.objects.all(), ...)
  • When creating questions, you should create the instance of the question type it is. This means that a paragraph should be created with: paragraph = Paragraph(question_text="How are you?", question_type="Paragraph", options = None, order_number=1) followed by a paragraph.save().
  • We're going to discuss on Sunday whether these were really a good idea. It might just be better to store the question_type in the Question model and get rid of these, but we'll talk about it Sunday
Clone this wiki locally