summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2013-07-23 15:06:02 +0300
committerAnssi Kääriäinen <akaariai@gmail.com>2013-07-23 15:37:26 +0300
commit43f1d51b4ba3ab2c0223928904e1d24c0571da8d (patch)
tree6dc239e526587359c416e4866d88f322878d11a7
parent92f66a613498172e6300c8a0b57513183e998597 (diff)
[1.6.x] Minor change to get_extra_descriptor_filter()
Refs #20611. Backpatch of 6b4967e88368934dbbb1f289c790ab813fa59c72.
-rw-r--r--django/db/models/fields/related.py16
1 files changed, 11 insertions, 5 deletions
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index 0367d24fe8..650bbdfe73 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -297,10 +297,15 @@ class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjec
params = dict(
(rh_field.attname, getattr(instance, lh_field.attname))
for lh_field, rh_field in self.field.related_fields)
- params.update(self.field.get_extra_descriptor_filter(instance))
qs = self.get_queryset(instance=instance)
+ extra_filter = self.field.get_extra_descriptor_filter(instance)
+ if isinstance(extra_filter, dict):
+ params.update(extra_filter)
+ qs = qs.filter(**params)
+ else:
+ qs = qs.filter(extra_filter, **params)
# Assuming the database enforces foreign keys, this won't fail.
- rel_obj = qs.get(**params)
+ rel_obj = qs.get()
if not self.field.rel.multiple:
setattr(rel_obj, self.field.related.get_cache_name(), instance)
setattr(instance, self.cache_name, rel_obj)
@@ -1003,10 +1008,11 @@ class ForeignObject(RelatedField):
user does 'instance.fieldname', that is the extra filter is used in
the descriptor of the field.
- The filter should be something usable in .filter(**kwargs) call, and
- will be ANDed together with the joining columns condition.
+ The filter should be either a dict usable in .filter(**kwargs) call or
+ a Q-object. The condition will be ANDed together with the relation's
+ joining columns.
- A parallel method is get_extra_relation_restriction() which is used in
+ A parallel method is get_extra_restriction() which is used in
JOIN and subquery conditions.
"""
return {}