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

AttributeError: module 'rest_framework.filters' has no attribute 'FilterSet' #3

Closed
yslhzj opened this issue Aug 23, 2018 · 9 comments
Closed
Assignees

Comments

@yslhzj
Copy link

yslhzj commented Aug 23, 2018

I was practicing Chapter08.
When I run the program Times the following error, please help fix it, thank you.

AttributeError: module restore _ framework.filters' has no attribute extension FilterSet'

Relevant information:
Djaog 1.11.11.
Python 3.6.
Djangorestframework-3.6.4

@Pomax
Copy link

Pomax commented May 3, 2019

Same - the reason is that FilterSet is part of django_filters, not rest_framekwork, so:

...
# remove the "from rest_framework import filters" line
...
from django_filters import FilterSet, AllValuesFilter, DateTimeFilter, NumberFilter 

... 

class CompetitionFilter(FilterSet): 
  ...

@Pomax
Copy link

Pomax commented Oct 5, 2020

@deepesh-packt did a PR fix this? If not, this is still going to cause problems for people following along with the text

@josmare
Copy link

josmare commented Dec 7, 2020

I kept playing until I got the correct references from here:

Here

The correct import in Views.py is like this:

from django_filters import rest_framework as filters

and the code for CompetitionFilteris like this

min_distance_in_feet = filters.NumberFilter( field_name='distance_in_feet', lookup_expr='gte') max_distance_in_feet = filters.NumberFilter( field_name='distance_in_feet', lookup_expr='lte')

@Ehsan-sudo
Copy link

I kept playing until I got the correct references from here:

Here

The correct import in Views.py is like this:

from django_filters import rest_framework as filters

and the code for CompetitionFilteris like this

min_distance_in_feet = filters.NumberFilter( field_name='distance_in_feet', lookup_expr='gte') max_distance_in_feet = filters.NumberFilter( field_name='distance_in_feet', lookup_expr='lte')

It worked for me! I was using "name" instead of "field_name" which caused the problem.

@carlosdelfino
Copy link

carlosdelfino commented Jan 7, 2023

I was having success adapting the contents of the book up to chapter 7, for django 4.1.5 and Python 3.10.6 when I ran into this same problem. I made the adjustments suggested here until my comment, but I see that the CompetitionFilter class is not having an effect, I tried to access the URL: http://localhost:8000/drones/competitions/?pilot_name=Penelope+Pitstop&drone_name=WonderDrone and all competitions are returned.

my code is available at: https://github.com/carlosdelfino/Django-RESTful-Web-Services

@carlosdelfino
Copy link

I found the solution to the problem, follow the path performed:

carlosdelfino/Django-RESTful-Web-Services@76bfbfc

@Pomax
Copy link

Pomax commented Jan 9, 2023

If you've forked the repo and updated the code to work, remember to file a PR so that the official repository can be updated, instead of telling people to use your repository.

@PaulPextra
Copy link

Please, also remember to rename "filter_fields" to "filterset_fields" in the classes with the List suffix (DroneCategoryList, DroneList, and PilotList), else request with filter query parameter will always return list of all the objects.

@smlnjoroge
Copy link

from django_filters import FilterSet, AllValuesFilter, DateTimeFilter, NumberFilter
from django_filters import rest_framework as filters

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

No branches or pull requests

9 participants