0

I'm using a DRF ModelSerializer to serve a one-field queryset, but the response returns as a list of dicts

[{"state": "AL"}, {"state": "AR"}, {"state": "AZ"}]

Is there any way to return a pure string list, like ["AL", "AR", "AZ"] ? I've explored other questions, but haven't found anything useful.

2
  • Wouldn't a SerializerMethodField work? Commented Sep 21, 2021 at 15:36
  • you can use python comprehension list to transform your dict to list of values and return it as json. Commented Sep 21, 2021 at 15:41

1 Answer 1

1

If you just need the state, you can extract the data out of that list of dicts:

response = [{"state": "AL"}, {"state": "AR"}, {"state": "AZ"}]
states = [data.get("state") for data in response]
print(states)

Output

['AL', 'AR', 'AZ']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.