-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed #35520 -- Used correct database for ModelAdmin
transactions
#18268
base: main
Are you sure you want to change the base?
Fixed #35520 -- Used correct database for ModelAdmin
transactions
#18268
Conversation
django/contrib/admin/options.py
Outdated
with transaction.atomic(using=router.db_for_write(self.model)): | ||
db = ( | ||
router.db_for_read(self.model) | ||
if request.method in ("GET", "HEAD", "OPTIONS", "TRACE") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There isn't currently a util for whether the request's method is read-only, so I've had to duplicate this set of methods a fair bit. Not sure this is the point we want to extract them into a util?
145f623
to
fefc3b7
Compare
fefc3b7
to
301e1a7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this @RealOrangeOne 👍
I have minor comments
301e1a7
to
d7f5bba
Compare
c094064
to
e43fa94
Compare
e43fa94
to
a05c7c3
Compare
Trac ticket number
ticket-35520
Branch description
In
ModelAdmin.delete_view
andModelAdmin.changelist_view
, the transaction is hardcoded to usedb_for_write
, even if the request is read-only (egGET
). This potentially results in the wrong database being selected (or errors ifdb_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
main
branch.