1
0
mirror of https://github.com/django/django.git synced 2025-08-14 13:59:12 +00:00

Minor change to get_extra_descriptor_filter()

Refs #20611.
This commit is contained in:
Anssi Kääriäinen 2013-07-23 15:06:02 +03:00
parent c00a8525c6
commit 6b4967e883

View File

@ -296,10 +296,15 @@ class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjec
params = dict( params = dict(
(rh_field.attname, getattr(instance, lh_field.attname)) (rh_field.attname, getattr(instance, lh_field.attname))
for lh_field, rh_field in self.field.related_fields) 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) 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. # 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: if not self.field.rel.multiple:
setattr(rel_obj, self.field.related.get_cache_name(), instance) setattr(rel_obj, self.field.related.get_cache_name(), instance)
setattr(instance, self.cache_name, rel_obj) 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 user does 'instance.fieldname', that is the extra filter is used in
the descriptor of the field. the descriptor of the field.
The filter should be something usable in .filter(**kwargs) call, and The filter should be either a dict usable in .filter(**kwargs) call or
will be ANDed together with the joining columns condition. 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. JOIN and subquery conditions.
""" """
return {} return {}