-
Notifications
You must be signed in to change notification settings - Fork 771
Queries for ModelMultipleChoiceFilter executed twice #1211
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
Comments
Hi @valentijnscholten. Could you possibly open a PR that demonstrates the bug? I've tried testing this with the models provided in the test suite, and have been unable to reproduce the issue. Here's the example test that I added to class GenericClassBasedViewTests(GenericViewTestCase):
...
def test_queries(self):
factory = RequestFactory()
request = factory.get('/')
class BookFilterSet(FilterSet):
lovers = ModelMultipleChoiceFilter(queryset=User.objects.all())
class Meta:
model = Book
fields = []
self.assertIn('lovers', BookFilterSet.base_filters)
view = FilterView.as_view(filterset_class=BookFilterSet)
with self.assertNumQueries(2):
response = view(request)
response.render() The above passes as expected, as it makes two queries - one to the books table, one to the users table (Note that I got the queries from changing the number of expected queries above). Captured queries were:
1. SELECT "tests_user"."id", "tests_user"."username", "tests_user"."first_name", "tests_user"."last_name", "tests_user"."status", "tests_user"."is_active", "tests_user"."is_employed" FROM "tests_user"
2. SELECT "tests_book"."id", "tests_book"."title", "tests_book"."price", "tests_book"."average_rating" FROM "tests_book" |
I could try, it's quite a big project with lots of different things mixed in together. Which might be the cause of the issue if simpler setups like the testcases are working fine. |
Going to close this for now, as it doesn't look like there's an issue with django-filter itself. |
Did some testing again now that 2.3.0 was out, but the issue remains. However, it's indeed not an issue with |
Hi,
I am seeing that for every field that is a foreign key, that every query used to populate the dropdown list is executed twice.
I have stripped down my big filter down to the simplified filter below, and I am still seeing 2 identical queries for prod_type.
Models:
Filter:
In a function based view:
filter = ProductFilter2(request.GET, queryset=prods, user=request.user)
Template:
Queries observed with django debug toolbar:
The above are just examples, I have at least 5 other models and as many foreign key fields and all seem to trigger two queries per field.
It could be something I am overlooking, but I can't almost make my example any simpler. When I remove the prod_type field or remove the form.as_p from the template, both queries dissappear.
It happens both with autogenerated filters when using Meta model + fields, but also with a manual filter like the above.
requirements.txt:
Both queries have an identical stacktrace/traceback:
The text was updated successfully, but these errors were encountered: