-
Notifications
You must be signed in to change notification settings - Fork 1
Models
Question.question_text
The text that the question will display.
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.
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.
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)
Note: This is just showing how to manipulate the underlying data types of a Question object. We have functions specifically made for modifying the options list of a Question object so that you don't have to deal with the string representation. Use those functions instead!
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.
We can enforce uniqueness later.
To select all of the questions use Question.objects.all()
. You can also query specific question types by using Paragraph.objects.all()
, Radiobutton.objects.all()
, Checkbox.objects.all()
, and Dropdown.objects.all()
.
The best way to determine the type of a question type is to use each specific question type's isinstance helper function. Example:
>>> from portal.models import Radiobutton, Checkbox
>>> q1 = Radiobutton.create("This is a radio button question", ["Yes", "No"])
>>> q2 = Dropdown.create("This is a dropdown question", ["Yeah", "Nah"])
>>> Radiobutton.isinstance(q1)
True
>>> Radiobutton.isinstance(q2)
False
>>> Dropdown.isinstance(q1)
False
>>> Dropdown.isinstance(q2)
True
- 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 aparagraph.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 theQuestion
model and get rid of these, but we'll talk about it Sunday