From 99b9bfc3efed3dad338309a4d2d01956484169e8 Mon Sep 17 00:00:00 2001 From: David <36340871+davidslusser@users.noreply.github.com> Date: Tue, 8 Feb 2022 18:13:12 -0800 Subject: [PATCH] fixes https://github.com/jazzband/django-eav2/issues/163 (#164) * fixes https://github.com/jazzband/django-eav2/issues/163 * chore: revert content to master * fix: don't expand relational fields * chore: ignore .vscode * chore: update changelog * chore: remove stale code Co-authored-by: Mike Co-authored-by: Mike <22396211+Dresdn@users.noreply.github.com> --- .gitignore | 5 +++++ CHANGELOG.md | 2 ++ eav/queryset.py | 13 ++----------- tests/test_queries.py | 8 +++++++- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index e7c4913..60b68e6 100644 --- a/.gitignore +++ b/.gitignore @@ -126,3 +126,8 @@ tags ## Mac .DS_Store + + +## IDE +.idea +.vscode diff --git a/CHANGELOG.md b/CHANGELOG.md index c4f7c6d..3020e13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ We follow [Semantic Versions](https://semver.org/) starting at the `0.14.0` rele ### Bug Fixes +- Fixes FieldError when filtering on foreign keys [#163](https://github.com/jazzband/django-eav2/issues/163) + ### Misc ## 1.2.0 (2021-12-18) diff --git a/eav/queryset.py b/eav/queryset.py index 300932e..6a53e99 100644 --- a/eav/queryset.py +++ b/eav/queryset.py @@ -248,17 +248,8 @@ def expand_eav_filter(model_cls, key, value): return '%s__in' % gr_name, value - try: - field = model_cls._meta.get_field(fields[0]) - except FieldDoesNotExist: - return key, value - - if not field.auto_created or field.concrete: - return key, value - else: - sub_key = '__'.join(fields[1:]) - key, value = expand_eav_filter(field.model, sub_key, value) - return '{}__{}'.format(fields[0], key), value + # Not an eav field, so keep as is + return key, value class EavQuerySet(QuerySet): diff --git a/tests/test_queries.py b/tests/test_queries.py index 109d50b..ba2fb32 100644 --- a/tests/test_queries.py +++ b/tests/test_queries.py @@ -6,7 +6,7 @@ from django.test import TestCase import eav from eav.models import Attribute, EnumGroup, EnumValue, Value from eav.registry import EavConfig -from test_project.models import Encounter, Patient +from test_project.models import Encounter, Patient, ExampleModel class Queries(TestCase): @@ -292,3 +292,9 @@ class Queries(TestCase): eav.unregister(Patient) eav.register(Patient, config_cls=CustomConfig) self.assert_order_by_results(eav_attr='data') + + def test_fk_filter(self): + e = ExampleModel.objects.create(name='test1') + p = Patient.objects.get_or_create(name='Beth', example=e)[0] + c = ExampleModel.objects.filter(patient=p) + self.assertEqual(c.count(), 1)