carltongibson / django-filter

A generic system for filtering Django QuerySets based on user selections
https://django-filter.readthedocs.io/en/main/
Other
4.44k stars 767 forks source link

Use empty MultiValueDict for default value of filterset data #1633

Closed sgordon16 closed 7 months ago

sgordon16 commented 9 months ago

Issue Summary

When using the filterset with Django and passing empty request parameters, the default value for data is set to an empty dict ({}) instead of a subclass of MultiValueDict. This becomes problematic for code that relies on methods specific to MultiValueDict, such as getlist.

Reproduction Steps

  1. Create a Django filterset instance.
  2. Send a request with empty parameters.
  3. Observe that the data attribute in the filterset is set to an empty dict.

Expected Behavior

The data attribute in the filterset should be set to a subclass of MultiValueDict, even when the request parameters are empty. This ensures that methods like getlist can be used consistently.

Actual Behavior

The data attribute is set to a regular empty dict ({}), causing issues for code that expects a MultiValueDict instance.

Code Example

# Code snippet that breaks due to the issue
self.filterset.data.getlist("a")
*** AttributeError: 'dict' object has no attribute 'getlist'    

Proposed Solution

Consider modifying the default value for data in the filterset to ensure it's always a subclass of MultiValueDict, even when the request parameters are empty.

carltongibson commented 9 months ago

@sgordon16 Can you open a PR to see what the test suite says?

carltongibson commented 7 months ago

Fixed in #1634