Posted: February 27th, 2010 | Author: Davo | Filed under: Django | Tags: db.connection, query, show queries, sql | No Comments »
The Django ORM is just awesome but for debugging purposes it is usefull to be able to see what queries are actually made to the database. It is possible to have a history of all the queries made until the request is finished. Additionaly the query object can be accessed to retrieve the SQL statement.
Importing the connection and printing the queries
>>> from django.db import connection
>>> print connection.queries
[]
>>> # as you can see it prints an empty list since no queries are made yet!
Make a call to the database and print again the queries
# I am using a model from my app. This is just an example.
>>> from apps.cobblers.models import Cobbler
>>> Cobbler.objects.filter()
[<Cobbler: 99 Dry Cleaners>]
>>> print connection.queries
[{'time': '0.000', 'sql': u'SELECT `cobblers_cobbler`.`id`, `cobblers_cobbler`.`name`, `cobblers_cobbler`.`description`, `cobblers_cobbler`.`address`, `cobblers_cobbler`.`city`, `cobblers_cobbler`.`post_code`, `cobblers_cobbler`.`telephone`, `cobblers_cobbler`.`email` FROM `cobblers_cobbler` ORDER BY `cobblers_cobbler`.`name` ASC LIMIT 21'}]
# the result is different, we have the latest query.
Finally it is also possible to see the query which will be executed for a given statement
>>> Cobbler.objects.filter(id=1).query
<django.db.models.sql.query.Query object at 0x8940c2c>
>>> str(Cobbler.objects.filter(id=1).query)
'SELECT `cobblers_cobbler`.`id`, `cobblers_cobbler`.`name`, `cobblers_cobbler`.`description`, `cobblers_cobbler`.`address`, `cobblers_cobbler`.`city`, `cobblers_cobbler`.`post_code`, `cobblers_cobbler`.`telephone`, `cobblers_cobbler`.`email ` FROM `cobblers_cobbler` WHERE `cobblers_cobbler`.`id` = 1 ORDER BY `cobblers_cobbler`.`name` ASC'
>>>
Posted: February 24th, 2010 | Author: Davo | Filed under: Django | Tags: getlist, querydict, request.post | 7 Comments »
The QueryDict.getlist() allows to get all the checkbox(or select list) values from the request.POST/GET object.
Let’s assume we have a simple form with the following checkboxes. Each checkbox contains an ID of an artist.
In views.py :
def handle(request):
if request.method == 'POST':
artists = request.POST.getlist('artists') # now artists is a list of [1,2,3]
Posted: February 24th, 2010 | Author: Davo | Filed under: Django | Tags: connect disconnect, Django, post_delete, post_save, pre_delete, pre_save, signals | No Comments »
Django signals allow to bind a function to a specific event.
There are 4 major signals available:
- pre_save() ( before saving )
- post_save() ( after saving )
- pre_delete() ( before deleting )
- post_delete() ( after deleting )
A simple example of connecting a signal:
# in models.py
from django.db.models.signals import post_save
from myapp.models import MyModel
def do_something(sender, **kwargs):
# the object which is saved can be accessed via kwargs 'instance' key.
obj = kwargs['instance']
print 'the object is now saved.'
# ...do something else...
# here we connect a post_save signal for MyModel
# in other terms whenever an instance of MyModel is saved
# the 'do_something' function will be called.
post_save.connect(do_something, sender=MyModel)
If you need to disconnect/detach a signal:
# instead of connect() we call disconnect()
post_save.disconnect(do_something, sender=MyModel)
Posted: February 22nd, 2010 | Author: Davo | Filed under: Django | Tags: Django, fields, get fields, list fields, meta fields, model | 8 Comments »
It is possible to get/list all of the fields for the given model, to do so you need to access the _meta attribute of the Model.
The following example demonstrates how to do it.
# in the models.py
class Car(models.Model)
year = models.IntegerField()
name = models.CharField(max_length=50)
...
def get_fields(self):
# make a list of field/values.
return [(field, field.value_to_string(self)) for field in Car._meta.fields]
Now that we implemented the get_fields function for our Car model, we can use this in the template…
# your template file...
# I assume that the variable 'car' is an instance of Car object.
{% for field, value in car.get_fields %}
{{ field }} : {{ value }}
{% endfor %}
Posted: February 20th, 2010 | Author: Davo | Filed under: Django | Tags: slugify, slugstring | 2 Comments »
The SlugField() converts the string to slug version but what about when we want to convert something to slug? To convert a string into a slug/slugified version, we can use the Django’s based slugify template tag.
>>> from django.template.defaultfilters import slugify
>>> msg = 'This Is Just a Random String to Be converted to SLUG strinG!'
>>> slugify(msg)
u'this-is-just-a-random-string-to-be-converted-to-slug-string'
>>>
As you can see all the spaces were converted to ‘-’ the ‘!’ was removed.