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

query name different with field name #237

Closed
Guest007 opened this issue Jun 29, 2018 · 3 comments
Closed

query name different with field name #237

Guest007 opened this issue Jun 29, 2018 · 3 comments

Comments

@Guest007
Copy link

installs django-rest-framework-filters (fresh from github)
python 3.6.5, django 2
This code is work.

class OrderFilter(filters.FilterSet):
    user = filters.AllLookupsFilter(name='user')

    class Meta:
        model = Order
        fields = ['user']

/api/v1/orders/order/?user=1 - works great.
but I need to use customer in query instead of real model name user:
/api/v1/orders/order/?customer=1
I tried this:

class OrderFilter(filters.FilterSet):
   customer = filters.AllLookupsFilter(name='user')

   class Meta:
       model = Order
       fields = ['customer']

And got:

    class OrderFilter(filters.FilterSet):
  File "/home/guest007/.pyenv/versions/3.6.5/envs/pex/src/djangorestframework-filters/rest_framework_filters/filterset.py", line 21, in __new__
    cls.expand_auto_filters(new_class)
  File "/home/guest007/.pyenv/versions/3.6.5/envs/pex/src/djangorestframework-filters/rest_framework_filters/filterset.py", line 42, in expand_auto_filters
    for gen_name, gen_f in new_class.get_filters().items():
  File "/home/guest007/.pyenv/versions/pex/lib/python3.6/site-packages/django_filters/filterset.py", line 280, in get_filters
    fields = cls.get_fields()
  File "/home/guest007/.pyenv/versions/3.6.5/envs/pex/src/djangorestframework-filters/rest_framework_filters/filterset.py", line 88, in get_fields
    fields[name] = utils.lookups_for_field(field)
  File "/home/guest007/.pyenv/versions/3.6.5/envs/pex/src/djangorestframework-filters/rest_framework_filters/utils.py", line 31, in lookups_for_field
    for expr, lookup in model_field.get_lookups().items():
AttributeError: 'NoneType' object has no attribute 'get_lookups'

where I was mistaken?

@rpkilby
Copy link
Collaborator

rpkilby commented Jun 29, 2018

The name argument should be field_name. See if that fixes it.

@Guest007
Copy link
Author

@rpkilby thank you. It's work.

@rpkilby
Copy link
Collaborator

rpkilby commented Jun 29, 2018

Yep - django-filter 1.1.0 should raise deprecation warnings about name being renamed to field_name. In 2.x, the warning and backwards compatibility was removed (see: carltongibson/django-filter#795).

Also, it's worth noting that the filter's attribute name is used as a default value when no field_name is explicitly provided. The first example works, because the filter's attribute name and the underlying model field name are the same. The latter didn't work, because customer is not a field on your model.

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

No branches or pull requests

2 participants