Categories
Python Answers

How to convert Python Django Model object to dict with all of the fields intact?

Spread the love

To convert Python Django Model object to dict with all of the fields intact, we can use the queryset’s values().first method.

For instance, we write

type(o).objects.filter(pk=o.pk).values().first()

to create a queryset with

type(o).objects.filter(pk=o.pk)

then we get the values returned by the query set with values.

And then we return the first item returned by values with the first method.

first returns the item as a dictionary.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *