#25212: Document RawSQL expression -------------------------------------+------------------------------------- Reporter: timgraham | Owner: timgraham Type: | Status: new Cleanup/optimization | Component: | Version: 1.8 Documentation | Severity: Normal | Keywords: Triage Stage: Accepted | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- from Anssi [https://groups.google.com/d/msg/django- developers/FojuU0syO8Y/YjUFY1jqCAAJ on django-developers]:
There doesn't seem to be direct documentation about RawSQL expression. The RawSQL is once mentioned in the documentation, but it definitely needs a bit more in the database functions documentation. [[BR]] The usage of RawSQL is really simple: `qs.annotate(val=RawSQL("(select col from sometable where othercol= %s)", (someparam,)))` [[BR]] This is equivalent to `qs.extra(select={'val': "select col from sometable where othercol = %s"}, select_params=(someparam,))` [[BR]] The main benefit of using RawSQL is that you can set output_field if needed. The main downside is that if you refer to some table alias of the qs in the raw sql, then it is possible that Django changes that alias (for example, when the qs is used as a subquery in yet another query). [[BR]] Combining raw SQL with alias relabeling support is possible, too with the following library: https://github.com/akaariai/django-refsql -- Ticket URL: <https://code.djangoproject.com/ticket/25212> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/052.e75b5a599773d2aab5dbbaca064de0e3%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.