Creating objects
To represent database-table data in Python objects, Django uses an intuitive system: a model class represents a database table, and an instance of that class represents a particular record in the database table.
To create an object, instantiate it using keyword arguments to the model class, then call save()
to save it to the database.
Assuming models live in a file mysite/blog/models.py
, here's an example:
>>> from blog.models import Blog >>> b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.') >>> b.save()
This performs an INSERT
SQL statement behind the scenes. Django doesn't hit the database until you explicitly call save()
.
The save()
method has no return value.
To create and save an object in a single step, use the create()
method.