Azad Rasul


7- Create a new question and choices

Azad Rasul's photo
Azad Rasul
·May 14, 2022·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

To invoke the Python shell, use this command:

py shell

Once you’re in the shell:

from polls.models import Choice, Question

Create a new Question.

from django.utils import timezone
q = Question(question_text="Rate this tutorial:",

Save the object into the database. You have to call save() explicitly.

Now it has an ID.

Access model field values via Python attributes.


'Rate this tutorial:'


datetime.datetime(2022, 5, 14, 15, 41, 26, 711723, tzinfo=datetime.timezone.utc)

Create five choices:

q.choice_set.create(choice_text='*', votes=0)
q.choice_set.create(choice_text='**', votes=0)
q.choice_set.create(choice_text='***', votes=0)
q.choice_set.create(choice_text='****', votes=0)
q.choice_set.create(choice_text='*****', votes=0)

Share this