Skip to content

Serialize the options from ModelMultipleChoiceFilter for use with DRF #1690

Discussion options

You must be logged in to vote

Hi @alexanderjulmer.

So... DRF with use django-filter via filter_backends to actually filter the request, and then the Browsable API will render the form in the HTML — but all of that is server-side.

To pass the choices to the client-side you'd need to serialise them yourself...

choices = filterset.form.fields["mymodel"].choices
for label, value in choices: 
    # Put them in the format you want for your frontend to consume. 
    # Either a mapping, or a pair of arrays, probably. 

... and then you send that to the frontend to build the form UI.

Make sense?

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@alexanderjulmer
Comment options

Answer selected by alexanderjulmer
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