Use Python Shell to Create, Save, and Update Objects in Your Database

By: Dusty Arlia
Published on Sunday, February 23, 2014, 04:30 PM
Last Updated on Friday, July 17, 2015 at 4:14 PM
Total Updates: 2

Invoke the Python shell with this command:

python manage.py shell

Create a person and add them into the database:

>>> p = Person(first_name="Justin", last_name="Timberlake")
>>> p.save()

The person will have an id (the id may be "1L" for "long integers" depending on the type of database you are using):

>>> p.id
1

Access database columns uing Python attributes:

>>> p.first_name
"Justin"

Change values by changing the attributes, then calling save().

>>> p.first_name = "Dustin"
>>> p.save()

Comments:

Ad:

Ad: