gorodechnyj
gorodechnyj

Reputation: 691

Django QuerySet way to select from sql table-function

everybody.

I work with Django 1.3 and Postgres 9.0. I have very complex sql query which extends simple model table lookup with some extra fields. And it wrapped in table function since it is parameterized.
A month before I managed to make it work with the help of raw query but RawQuerySet lacks a lot of features which I really need (filters, count() and clone() methods, chainability).
The idea looks simple. QuerySet lets me to perform this query:

SELECT "table"."field1", ... ,"table"."fieldN" FROM "table"

whereas I need to do this:

SELECT "table"."field1", ... ,"table"."fieldN" FROM proxy(param1, param2)

So the question is: How can I do this? I've already started to create custom manager but can't substitute model.db_table with custom string (because it's being quoted and database stops to recognize the function call).
If there's no way to substitute table with function I would like to know if I can create QuerySet from RawQuerySet (Not the cleanest solution but simple RawQuerySet brings so much pain in... one body part).

Upvotes: 0

Views: 784

Answers (1)

lprsd
lprsd

Reputation: 87105

If your requirement is that you want to use Raw SQL for efficiency purposes and still have access to model methods, what you really need is a library to map what fields of the SQL are the columns of what models.

And that library is, Unjoinify

Upvotes: 1

Related Questions