Django database-api

20 views
Skip to first unread message

KasunLak

unread,
Apr 3, 2012, 2:45:19 AM4/3/12
to Django users
Hi all,

I just wonder is there a way to browse/view the code of database api
methods generated? Is there any tool for this?

Thanks in advance,
Kasun

Eugenio Minardi

unread,
Apr 3, 2012, 3:10:06 AM4/3/12
to django...@googlegroups.com
Hi,
Do you mean the SQL generated by your model?

In this case you can use 

python manage.py sql APPNAME


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


KasunLak

unread,
Apr 3, 2012, 4:27:13 AM4/3/12
to Django users
Hi,

No, sorry for not giving more details. What I am asking is once we
syncdb the generated scripts (create table). Can we see the method
implementation of database access api?

For example: If we have created and deployed a model called Poll
(tutorial example in django site)

p = Poll.objects.get(pk=1).

Can we see the implementation of this get(pk) method? I think these
are some objects in the database, just wonder this is possible?

thanks

akaariai

unread,
Apr 3, 2012, 4:41:57 AM4/3/12
to Django users
On Apr 3, 11:27 am, KasunLak <kasun.lakpriy...@gmail.com> wrote:
> Hi,
>
> No, sorry for not giving more details. What I am asking is once we
> syncdb the generated scripts (create table). Can we see the method
> implementation of database access api?
>
> For example: If we have created and deployed a model called Poll
> (tutorial example in django site)
>
> p = Poll.objects.get(pk=1).
>
> Can we see the implementation of this get(pk) method? I think these
> are some objects in the database, just wonder this is possible?

If you mean what SQL it generates there are two methods inbuilt to
django. If you have settings.DEBUG = True, you can use:
> p = Poll.objects.get(pk=1).
> django.db import connection
> print connection.queries[-1]

Another option:
> print Poll.objects.filter(pk=1).query
This one does work only for QuerySet, so if the method you are
interested in executes the query immediately (like .get()), then you
can't use this method.

Neither of the above will give you correctly quoted querystring, as
that is simply not available to Django. The database backend libraries
do the quoting, and Django can't get the quoted query string back from
the libraries.

If you want to see the Python code implementation, look at django/db/
models/query.py, which uses django/db/models/sql/query.py. Be warned:
the sql/query.py will take some time to understand, it is one of the
most complex pieces of code in Django.

- Anssi

KasunLak

unread,
Apr 3, 2012, 5:12:59 AM4/3/12
to Django users
Hi,

Thanks you very much for the informative answer! It clears all the
questions I had in my mind. Because I have some idea to build a tool
to draw the model (UML) --> Generate Django Model code --> and then we
can deploy it to db. Moreover I was thinking that if it possible to
customize (edit existing methods, add new methods..) the generated
database api it would be good.

Thanks again,
Kasun
Reply all
Reply to author
Forward
0 new messages