Opened 7 years ago

Closed 3 years ago

#11521 closed Bug (fixed)

QuerySet.update() doesn't accept fkey assignment by id (with patch)

Reported by: Adys Owned by: Adys
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: adys.wh@…, timograham@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


QuerySet.update doesn't accept FIELD_id assignments for ForeignKeys. This prevents the user from using the same dict to create or update a key.

Attached patch solves that problem by adding attname keys in the field cache. It may or may not be a hack.

Attachments (1)

fkeyassignbyid.patch (608 bytes) - added by Adys 7 years ago.
Add attname to field cache

Download all attachments as: .zip

Change History (11)

Changed 7 years ago by Adys

Add attname to field cache

comment:1 Changed 7 years ago by Adys

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to Adys
  • Patch needs improvement unset

comment:2 Changed 7 years ago by Adys

Ping. Any feedback on this?

comment:3 Changed 6 years ago by russellm

  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 5 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:5 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:6 Changed 4 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:7 Changed 3 years ago by timo

  • Cc timograham@… added
  • Needs tests unset

comment:8 Changed 3 years ago by akaariai

As discussed in the pull request the change done to Options.init_name_map will effectively turn the allow_explicit_fk parameter useless. The change done is adding field.attname to the name map. The allow_explicit_fk parameter is used in join generation inside the sql/, and it allows using attname for lookups when the parameter is set. The parameter is set for filter() and values() calls at least, but isn't set for some other operations, like annotate(). I can't see any reason for the inconsistency - either allow attname everywhere or nowhere.

One test fails if the change to init_name_map and removal of allow_explicit_fk is done. The failing test is lookup.tests.LookupTests.test_error_messages, it fails because author_id is in the error message's "allowed choices" list in addition to author. But, that is actually correct, author_id is an allowed choice.

Any opposition for making this change at alpha stage? There is a minor risk this could break something.

The proposed change will also fix this ticket's issue. A patch is available from:

comment:9 Changed 3 years ago by timo

  • Triage Stage changed from Accepted to Ready for checkin

We can make this change for 1.7. Patch still applies cleanly and tests pass.

comment:10 Changed 3 years ago by Anssi Kääriäinen <akaariai@…>

  • Resolution set to fixed
  • Status changed from new to closed

In e01b5a5823fa06a63382f87472978a16c77048d2:

Fixed #11521 -- usage of field.attname in .update()

Fixed already by previous patch, only test added.

Note: See TracTickets for help on using tickets.
Back to Top