Skip to content

Instantly share code, notes, and snippets.

@jonwhittlestone
Created June 10, 2019 12:38
Show Gist options
  • Save jonwhittlestone/9fe4301a51b7317991e208bd2034ce3a to your computer and use it in GitHub Desktop.
Save jonwhittlestone/9fe4301a51b7317991e208bd2034ce3a to your computer and use it in GitHub Desktop.
Pretty printing a Django model instance by converting it to json
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