Skip to content

Conversation

zebra0345
Copy link
Contributor

Trac ticket number

#36391

Branch description

This PR adds documentation for using RawSQL in the "Performing raw SQL queries" guide (topics/db/sql.txt).

It introduces RawSQL as an ORM expression suitable for use in annotate() and filter(), with an example and a warning about SQL injection risks. The section complements existing documentation for Manager.raw() and cursor.execute().

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • I have attached screenshots in both light and dark modes for any UI changes.

Please let me know if any changes are needed — I’d appreciate suggestions for improving alignment with Django’s documentation tone and structure. Thank you!

Copy link
Member

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The introduction on lines 7-9 needs updating.

Line 215, which has a throwaway mention to Func, should be updated to mention RawSQL.

The new entry should be a ==== heading, like the other headings “Performing raw queries” and “Executing custom SQL directly”.

In terms of order, perhaps it would make sense to mention RawSQL first.

I don’t think we need a full explainer of how to use RawSQL, since it has documentation of its own.

I am not sure the extra warning is necessary, the entire page is about Raw SQL and passing pararmeters anyway. I notice there's already one warning under “Passing parameters into raw()”, perhaps we can move that warning up to the top of the page?

Copy link
Member

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above ^

@zebra0345 zebra0345 requested a review from adamchainz May 26, 2025 15:32
@zebra0345
Copy link
Contributor Author

Thank you for the thoughtful feedback!

I've incorporated all your suggestions
Let me know if there's anything else you'd like me to improve. Thanks again!

Copy link
Member

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The introduction on lines 7-9 needs updating.

Still needs doing.

Line 215, which has a throwaway mention to Func, should be updated to mention RawSQL.

The way you updated this section doesn't point out that RawSQL is covered next.

Also, I now think that maybe the Func reference could be merged into the section we're writing, since that is another way of using "raw" SQL, via a short SQL fragment. Perhaps we could call the section "Raw SQL fragments" or similar.

In terms of order, perhaps it would make sense to mention RawSQL first.

This is still not actioned. I meant to put the section first in the document.

perhaps we can move that warning up to the top of the page?

Still needs doing, or at least discussing!

Comment on lines 221 to 222
Using RawSQL in ORM expressions
================================
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Using RawSQL in ORM expressions
================================
Using RawSQL in ORM expressions
===============================

Underlines need to match title length exactly in reStructuredText

Comment on lines 224 to 226
Django provides a number of built-in expressions and functions for use in queries.
However, when these are not sufficient, you can use :class:`~django.db.models.expressions.RawSQL`
to include custom SQL fragments inside annotations or filters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This intro to the topic doesn't really place RawSQL within the context of the surrounding page. It should highlight how RawSQL differs to the other options.

@zebra0345
Copy link
Contributor Author

Thank you for the detailed and thoughtful feedback - I've now addressed all the suggestions you've mentioned.

I've updated the introduction to better contextualize RawSQL in relation to the other raw query methods, revised the section ordering, and merged the Func reference into the new "Raw SQL fragments" section as advised. I’ve also adjusted the heading underline and moved the warning to the top of the page as discussed.

This is my first time contributing to the Django documentation, so I may still have missed some things. I’d really appreciate any further feedback you might have.

Thanks again for your guidance!

@zebra0345 zebra0345 requested a review from adamchainz May 28, 2025 10:14
@zebra0345
Copy link
Contributor Author

Dear @adamchainz,

Would you be willing to review this pull request (#19475) when you have a moment?

The proposed changes aim to improve the documentation by clarifying how RawSQL can be used within ORM expressions, as discussed in issue #36391. The patch is minimal, and all CI checks have passed.

Your feedback would be greatly appreciated. Thank you in advance for your time and your continued contributions to the Django project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants