Skip to content

Commit

Permalink
Merge pull request #41 from stphivos/fix/onetomany_field_lookup
Browse files Browse the repository at this point in the history
Fix one-to-many field lookup to use model name
  • Loading branch information
stphivos authored Mar 13, 2017
2 parents 53a967e + 4d415ef commit 0c38e37
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions django_mock_queries/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def aggregate(*args, **kwargs):
expr_result = None

for x in items:
val = get_attribute(x, expr.source_expressions[0].name, related_set_singular=True)[0]
val = get_attribute(x, expr.source_expressions[0].name)[0]
if val is None:
continue
values.extend(val if is_list_like_iter(val) else [val])
Expand Down Expand Up @@ -246,7 +246,7 @@ def values(*fields):
result_count = 1

for field in sorted(field_names, key=lambda k: k.count('__')):
value = get_attribute(item, field, related_set_singular=True)[0]
value = get_attribute(item, field)[0]

if is_list_like_iter(value):
value = flatten_list(value)
Expand Down
2 changes: 1 addition & 1 deletion django_mock_queries/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def names(field):
name = field.get_accessor_name()
model_name = field.related_model._meta.model_name.lower()

if kwargs.get('related_set_singular', False) and name[-4:] == '_set':
if name[-4:] == '_set':
return {model_name: name}
else:
return {name: name}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def test_is_match_in_children(self):
manufacturer.car_set.add(car)
Manufacturer.objects.add(manufacturer)

car_manufacturers = Manufacturer.objects.filter(car_set=car)
car_manufacturers = Manufacturer.objects.filter(car=car)

self.assertEqual([manufacturer], list(car_manufacturers))

Expand Down

0 comments on commit 0c38e37

Please sign in to comment.