views:

53

answers:

3

Hi, i'm triyng to make a full text search with postgresql and django So I've created a function search_client(text) which returns a list of clients. To call it from the DB i use something like this:

SELECT * FROM search_client('something')

and i'm not really sure how to call it from django. i know i could do something like

cursor = connection.cursor()
cursor.execute("SELECT * FROM search_client('something')")
result = cursor.fetchall()

but that will only return a list of values, and i'd like to have a list of objects, like when i use the "filter()" method.

Any ideas?? thanks for your time!

+1  A: 

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.

S.Lott
Database functions are not only for speed. Full text search is a good candidate for a function, makes optimizationa bit easier, you can do it inside your database.
Frank Heikens
well i'm not really doing it because i think it will be faster, i've created a function because inside the function i do a lot of things, including joins to other 5 tables, loops,etc. Things that could be a nightmare for me if i try to do it in python. Thxs
pleasedontbelong
@pleasedontbelong: "including joins to other 5 tables" -- Trivial in Django. "loops,etc." -- Trivial in Python, often faster than in the database. "Things that could be a nightmare for me if i try to do it in python" -- Can't understand this at all.
S.Lott
@Frank: "Full text search is a good candidate for a function, makes optimizationa bit easier". Optimization is for speed, correct? That leaves me confused by "Database functions are not only for speed". The **only** possible reason for stuffing code into the DB is speed. i.e., optimization of query processing.
S.Lott
Using a function will benefit all users of this function without changing code in all different applications. This is not only good for query processing, also for maintenance costs. That's why you could put complex queries inside a function, future maintenance will be cheaper and current development won't have extra costs.
Frank Heikens
@Frank Heikens: When writing a Django web application, who are "all users"? The Django ORM framework seems to assure that all possible use cases are handled through Python code. I'm unclear on how a stored procedure helps when it's hard to invoke and may not be any faster than Python.
S.Lott
I'm working in environments where lots off applications in many languages use the same databases. Our Java applications use Hibernate and that's not a problem with functions. ORM can be of help, but it's not the only tool you have.
Frank Heikens
@Frank Heikens: "I'm working in environments where lots off applications in many languages use the same databases." I'm unclear on how that applies to this question.
S.Lott
ORM is very limited in it's SQL statements, doesn't know all specific PostgreSQL-functions. Full text search is one example, you have to hardcode this in your code: http://barryp.org/blog/entries/postgresql-full-text-search-django/ You have to do the same thing in Hibernate and Propel. This is no fun if you have a better idea about how to search and get better results, have to fix one problem in 3 places. Just a single database function can do the trick as well, just a single place to maintain code. ORM is good for standard SQL, not for extensions.
Frank Heikens
wow i started a debate here. Maybe the point of view of a begginer (me) might give you a clear view... I'd rather use a function because today i'm working with python, but tomorrow i'll use php or even java, so if i dont use a function, each time i'd have to "re-create" the procedure. and this is not the only exemple.. for exemple triggers are pieces of code that fits nice in the DB. When i said that i'd be a nightmare for me it's because the query was so complex that i had no idea how to recreate it with django's ORM
pleasedontbelong
@pleasedontbelong: "triggers are pieces of code that fits nice in the DB."? Really? From the number of problems I've seen, that's not universally true. "query was so complex that i had no idea how to recreate it with django's ORM" Ask **that** question, then. I don't see a compelling case for stored procedures here.
S.Lott
+1  A: 

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

stevejalim
thank you, i'll use extra() cause' i'm using django 1.1
pleasedontbelong
+1  A: 

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')")
piquadrat