Login

Serialize a model instance

Author:
chriswedgwood
Posted:
February 18, 2021
Language:
Python
Version:
2.1
Tags:
serialize
Score:
2 (after 2 ratings)

Sometimes you need to see the json representation of an object, here is how I do it :)

1
2
3
4
5
6
from django.core import serializers
from .models import YourAwesomeModel

obj = YourAwesomeModel.objects.first()
serialized_obj = serializers.serialize('json', [ obj, ])
print(serialized_obj)

More like this

  1. "Magic Link" Management Command by webology 3 weeks, 5 days ago
  2. Closest ORM models to a latitude/longitude point by simonw 3 weeks, 5 days ago
  3. Log the time taken to execute each DB query by kennyx46 3 weeks, 5 days ago
  4. django database snippet by ItsRLuo 1 month ago
  5. Serialize a model instance by chriswedgwood 2 months ago

Comments

Please login first before commenting.