Hi Suriya,

It sounds like you're looking for raw SQL queries:

https://docs.djangoproject.com/en/1.8/topics/db/sql/#performing-raw-queries

This allows you to issue a SQL query in SQL, rather than trying to bend the
ORM to meet some complex query requirement.

You can't compose a raw query like a normal Django ORM query (e.g., you
can't add a filter clause to an raw query), but a raw query object behaves
exactly like queryset when it returns results - it is iterable, it returns
full Django objects, and so on.

Yours,
Russ Magee %-)

On Sun, May 3, 2015 at 9:22 AM, Suriya Subramanian <sur...@gmail.com> wrote:

> Hello,
>
> I have to write some complex SQL queries that I am unable to express using
> the ORM. I construct these complex queries by writing a few simple ORM
> queries, getting the SQL using QuerySet.query and combining them with
> various SQL operators manually. These hand-crafted queries are not very
> flexible because it is very easy to modify the final SQL.
>
> My question: Is there a way to programmatically construct the complex
> queries. I see that I can get the generated SQL and parameters by invoking
> SQLCompiler.as_sql(). Can I invoke as_sql() on the individual query sets
> and then construct the complex query? What are some gotchas that I need to
> watch out for?
>
> Thanks,
> Suriya Subramanian
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/b4465893-6d08-4ed3-babd-1f6e0f0f52ab%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/b4465893-6d08-4ed3-babd-1f6e0f0f52ab%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAJxq8492YZY_Px2o1nEKzF3-F3TBsv6ZGzs71zhkNmAGoRoNkQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to