django
django copied to clipboard
Fixed #35520 -- Used correct database for `ModelAdmin` transactions
Trac ticket number
ticket-35520
Branch description
In ModelAdmin.delete_view and ModelAdmin.changelist_view, the transaction is hardcoded to use db_for_write, even if the request is read-only (eg GET). This potentially results in the wrong database being selected (or errors if db_for_write is used to prevent writing to models).
This PR uses db_for_read for any read requests, based on the method. Most users won't notice the difference, as the same database will be selected. But those who are using a custom router will get the database they expect.
Checklist
- [x] This PR targets the
mainbranch. - [x] The commit message is written in past tense, mentions the ticket number, and ends with a period.
- [x] I have checked the "Has patch" ticket flag in the Trac system.
- [x] 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.