Login

Serialize a model instance

Author:
chriswedgwood
Posted:
February 18, 2021
Language:
Python
Version:
2.1
Score:
3 (after 3 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 4 months, 2 weeks ago
  2. Closest ORM models to a latitude/longitude point by simonw 4 months, 2 weeks ago
  3. Log the time taken to execute each DB query by kennyx46 4 months, 2 weeks ago
  4. django database snippet by ItsRLuo 4 months, 2 weeks ago
  5. Serialize a model instance by chriswedgwood 5 months, 2 weeks ago

Comments

Please login first before commenting.