Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering by related model with custom related_query_name doesn't work #146

Open
kvbrg opened this issue Sep 17, 2021 · 1 comment
Open
Labels

Comments

@kvbrg
Copy link

kvbrg commented Sep 17, 2021

Suppose we have two models: Patient and Slot.

class Slot(Model):
   ...
   email = EmailField(...)
   patient = models.ForeignKey(
       'patients.Patient',
       related_name='slots',
       related_query_name='slot',
       ...
    )

And then we need to filter patients by related slot.

def get_patients():
   ...
   # patients is queryset of patients.Patient
   return patients.filter(slot__email__isnull=True)

It's simple and working code. But when we try to mock get_patients with MockSet of patients created with fabric we get an error.

django.core.exceptions.FieldError: Cannot resolve keyword 'slot' into field. Choices are ...'slots', ...

If we change filter to slots__email__isnull=True, it works in tests. But now we get an error in Django shell.

django.core.exceptions.FieldError: Cannot resolve keyword 'slots' into field. Choices are: ... 'slot', ...

It would be great to fix this! Thanks. 😊

@stefan6419846
Copy link
Collaborator

I have not completely debugged this, but is seems like

def find_field_names(obj, **kwargs):
if hasattr(obj, '_meta'):
lookup_fields, actual_fields = find_field_names_from_meta(obj._meta, **kwargs)
else:
lookup_fields, actual_fields = find_field_names_from_obj(obj, **kwargs)
return lookup_fields, actual_fields
needs some reworking.

The actual stacktrace:

Traceback (most recent call last):
  File "/home/stefan/temp/intro/mysite/polls/tests/test_default.py", line 43, in test2
    print(Question.objects.filter(slot__isnull=False))
  File "/home/stefan/temp/intro/venv/lib/python3.6/site-packages/django_mock_queries/query.py", line 126, in filter
    return MockSet(*matches(*results, **attrs), clone=self)
  File "/home/stefan/temp/intro/venv/lib/python3.6/site-packages/django_mock_queries/utils.py", line 213, in matches
    disqualified = [x for x in source if is_disqualified(x, attrs, negated)]
  File "/home/stefan/temp/intro/venv/lib/python3.6/site-packages/django_mock_queries/utils.py", line 213, in <listcomp>
    disqualified = [x for x in source if is_disqualified(x, attrs, negated)]
  File "/home/stefan/temp/intro/venv/lib/python3.6/site-packages/django_mock_queries/utils.py", line 202, in is_disqualified
    attr_value, comparison = get_attribute(obj, attr_name)
  File "/home/stefan/temp/intro/venv/lib/python3.6/site-packages/django_mock_queries/utils.py", line 124, in get_attribute
    validate_field(attr_part, lookup_fields)
  File "/home/stefan/temp/intro/venv/lib/python3.6/site-packages/django_mock_queries/utils.py", line 91, in validate_field
    raise FieldError(message)
django.core.exceptions.FieldError: Cannot resolve keyword 'slot' into field. Choices are 'choice', 'id', 'pub_date', 'question_text', 'slots'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants