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. codigo alto nivel by MrRocklion 6 days, 3 hours ago
  2. Load template from specific app by Krzysiek555 3 weeks, 5 days ago
  3. PostgreSQL JSON subqueries by dolamroth 4 weeks ago
  4. "Magic Link" Management Command by webology 6 months ago
  5. Closest ORM models to a latitude/longitude point by simonw 6 months ago

Comments

chriswedgwood (on August 11, 2021):

test

#

Please login first before commenting.