ref: http://www.djangobook.com/en/2.0/chapter05.html
Commands:
To Validate the model:
To Show the SQL that will be generate
To Create the Database:
Instructions:
Create a new registry:
Create a new object using a model class, for instance (class Publisher) and call the function save( ).
Getting the generated ID:
After save an object, you can get the id using the id property of object:
where p1 is the object
Getting all rows in the table:
Adding the model string representations (like toString( )):
Add the function def __unicode__(self) into the model
Updating a registry:
First of all, the object needs to have a link with the database, and it occurs after you save the object into database or retrieve it. So, you can change the parameters of the object and call the function save( )
Filtering data: (returning QuerySet)
- Filtering by an argument (field):
- You can pass multiple arguments into filter() to narrow down things further:
Many other types of lookups are available, including icontains (case-insensitive LIKE), startswith and endswith, andrange (SQL BETWEEN queries)
Retrieving single objects:
- Filtering by an argument (field):
- if this query return more than one result, it will cause an expection ( MultipleObjectsReturned )
- if this query returns no objects also causes an exception ( DoesNotExist ), you can use the structure below to trap the DoesNotExist exception:
Ordering data:
- Ordering by argument using order_by( )
- Ordering by more than one argument using order_by( )
- Reverse Ordering using order_by. Uses the '-' sign in front of the argument
- Change the default model ordering
Filter and Ordering data:
Slicing Data
You can get only selected rows using the Python's standard list slicing syntax ([start:end]:
-Only the first object:
- Using Range:
- Change the default model ordering
Filter and Ordering data:
Slicing Data
You can get only selected rows using the Python's standard list slicing syntax ([start:end]:
-Only the first object:
- Using Range:
No comments:
Post a Comment