Database Reference
In-Depth Information
You have customized the powerful automatic administrative interface to quickly
assemble the back office pages of your application. Using the Django URL Dis-
patcher ,youhavedefinedtheURLroutesforyourapplicationinaconcisemanner.
Asyoumayhavenoticed,whatisextremelyniceabouttheDjangoabstractionisthe
automatic implementation of the data-access layer API using the models. You can
nowadd,update,delete,andqueryrecordsusingPythoncode,withouthavingany
knowledgeofSQL.TrythisyourselfusingtheDjangoPythonshell;youwillselectan
animalfromthedatabase,addanewsightingforthatanimal,andthenfinallydelete
thesighting.YoucaninvestigatetheSQLgeneratedbyDjangobehindthescenesat
any time, using the django.db .connection class, with the following command:
(chp09-env-bis)$ python manage.py shell
>>> from django.db import connection
>>> from datetime import datetime
>>> from sightings.models import Sighting,
Animal
>>> an_animal = Animal.objects.all()[0]
>>> an_animal
<Animal: Lion>
>>> print connection.queries[-1]['sql']
SELECT "sightings_animal"."id",
"sightings_animal"."name",
"sightings_animal"."image" FROM
"sightings_animal" ORDER BY
"sightings_animal"."name" ASC LIMIT 1'
>>> my_sight = Sighting(date=datetime.now(),
description='What a lion I have seen!', rate=1,
animal=an_animal, geometry='POINT(10 10)')
>>> my_sight.save()
print connection.queries[-1]['sql']
INSERT INTO "sightings_sighting" ("date",
"description", "rate", "animal_id", "geometry")
VALUES ('2013-06-12 14:37:36.544268-05:00',
'What a lion I have seen!', 1, 2,
ST_GeomFromEWKB('\x0101000020e610000000000000000024400000000000002440'::bytea))
RETURNING "sightings_sighting"."id"
>>> my_sight.delete()
Search WWH ::




Custom Search