Created
June 10, 2019 12:38
-
-
Save jonwhittlestone/9fe4301a51b7317991e208bd2034ce3a to your computer and use it in GitHub Desktop.
Pretty printing a Django model instance by converting it to json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core import serializers | |
from product.models import Product, Author | |
import json | |
# Pretty Printing a model instance by converting it to JSON | |
# assuming obj is a model instance | |
author_id = 6417 | |
products = Product.objects.filter(author_id=author_id) | |
serialized_obj = serializers.serialize('json', [ products[0], ]) | |
print(json.dumps(json.loads(serialized_obj), indent=2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment