Skip to content

Fix filterset to be unbound when no GET parameters are passed#81

Closed
marciomazza wants to merge 1 commit intocarltongibson:mainfrom
marciomazza:main
Closed

Fix filterset to be unbound when no GET parameters are passed#81
marciomazza wants to merge 1 commit intocarltongibson:mainfrom
marciomazza:main

Conversation

@marciomazza
Copy link

@marciomazza marciomazza commented Apr 23, 2025

Fix the filterset to be unbound on a request with no get parameters.

This is necessary to set the filterset's (and it's form) initial values.

See
https://github.com/carltongibson/django-filter/blob/635343ec55c9928bfa297314711df77fa83ff6c7/django_filters/filterset.py#L198

@carltongibson
Copy link
Owner

Hi @marciomazza — thanks for the PR.

... necessary to set the filterset's (and it's form) initial values.

Can you explain your use-case in more detail please? I have a suspicion this isn't the change you're looking for, but I'd like to hear your side of it fully first. Thanks.

@marciomazza
Copy link
Author

Oh... never mind.

I was trying set an initial value on a Filterset's field doing the following. Now I realize it doesn't work, even with this PR:

class EventFilter(django_filters.FilterSet):
    date = django_filters.DateFilter(
        field_name="event_date",
        widget=forms.DateInput(attrs={"type": "date"}),
        initial=date.today,
    )

    class Meta:
        model = Event
        fields = ["date"]

This, though, works (without the PR):

class EventFilter(django_filters.FilterSet):
    date = django_filters.DateFilter(
        field_name="event_date",
        widget=forms.DateInput(attrs={"type": "date"}),
    )

    class Meta:
        model = Event
        fields = ["date"]

    def __init__(self, data=None, *args, **kwargs):
        if not data:
            data = {"date": date.today()}
        super().__init__(data, *args, **kwargs)

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

Successfully merging this pull request may close these issues.

2 participants