Proper way to call a database function from Django? Proper way to call a database function from Django? django django

Proper way to call a database function from Django?


If your goal is a full-featured search engine, have a look at django-haystack. It rocks.

As for your question, the new (Django 1.2) raw method might work:

qs = MyModel.objects.raw("SELECT * FROM search_client('something')")


If you're using Django 1.2, you can use the raw() ORM method to execute custom SQL but get back Django models. If you're not, you can still execute the SQL via the extra() method on default QuerySet, and pump it into a custom method to either then go pull the real ORM records, or make new, temporary, objects


First, you probably don't want to do this. Do you have proof that your database function is actually faster?

Implement this in Python first. When you can prove that your Python implementation really is the slowest part of your transaction, then you can try a stored procedure.

Second, you have the extra method available in Django.

http://docs.djangoproject.com/en/1.2/ref/models/querysets/#django.db.models.QuerySet.extra

Note that compute-intensive database procedures are often slow.