Skip to content

Validate filter params #1719

Answered by isaac-blanc
isaac-blanc asked this question in Q&A
Jul 28, 2025 · 1 comments · 6 replies
Discussion options

You must be logged in to vote

Just got back to this. Have extended FilterSet as shown below. As far as I can tell, it works great. Would be grateful if you could cast an eye over it, although of course no worries if you haven't got the time. Thanks for your help!

class SafeFilterSet(FilterSet):
    
    def is_valid(self):
        expected_fields = set(self.form.fields)
        received_fields = set(self.request.GET.keys())
        
        if unexpected_fields := received_fields - expected_fields:
            raise ValueError(f'Received unexpected filter fields: {unexpected_fields}')
        
        return super().is_valid()

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@carltongibson
Comment options

@isaac-blanc
Comment options

@carltongibson
Comment options

@isaac-blanc
Comment options

Answer selected by isaac-blanc
@carltongibson
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants