Opened 11 years ago

Last modified 4 months ago

#11541 new Bug

F() expressions don't allow assignment of Foreign Key values on instances

Reported by: Russell Keith-Magee Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1-beta
Severity: Normal Keywords:
Cc: Abhijeet Viswa Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Lines 93-102 of the expressions model test define the following test case:

# F expressions cannot be used to update attributes which are foreign keys, or
# attributes which involve joins.
>>> test_gmbh.point_of_contact = None
>>> test_gmbh.save()
>>> test_gmbh.point_of_contact is None
True
>>> test_gmbh.point_of_contact = F('ceo')
Traceback (most recent call last):
...
ValueError: Cannot assign "<django.db.models.expressions.F object at ...>": "Company.point_of_contact" must be a "Employee" instance.

There's no reason this sort of assignment shouldn't be possible - it just requires the appropriate handling on the related field.

Change History (9)

comment:1 Changed 11 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted
Version: 1.01.1-beta-1

comment:2 Changed 11 years ago by dc

Saving FileField and ImageField with assigned F expression will also fail with

AttributeError: 'F' object has no attribute 'field'

comment:3 Changed 9 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:4 Changed 8 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:5 Changed 8 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:6 Changed 5 months ago by Abhijeet Viswa

I gave a shot at fixing this ticket. However, I got stuck real fast. F expressions aren't resolved until the queryset is evaluated. However, the test to check if the right-value is an instance of the forward relation's class is performed during assignment. The only solution I could think of is to defer the test for F expressions until queryset evaluation.

Maintainers, would this be the best approach?

comment:7 Changed 5 months ago by Abhijeet Viswa

Cc: Abhijeet Viswa added

comment:8 Changed 5 months ago by Simon Charette

The only solution I could think of is to defer the test for F expressions until queryset evaluation.

Abhijeet, the check can likely be deferred if hasattr(value, resolve_expression).

comment:9 Changed 4 months ago by Abhijeet Viswa

Marten Kenbeek pointed out to me that Fexpression can be assigned to the attname attribute of the Model instance. So, test_gmbh.point_of_contact_id can be assigned F('ceo') to have the intended effect. However, I don't think this is documented. Would documenting this be enough to close this ticket? Or should F expressions be assignable to the relation descriptors?

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