Login

Serialize a model instance

Author:
chriswedgwood
Posted:
February 18, 2021
Language:
Python
Version:
2.1
Tags:
serialize
Score:
1 (after 1 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. Serialize a model instance by chriswedgwood 1 week, 6 days ago
  2. Automatically setup raw_id_fields ForeignKey & OneToOneField by agusmakmun 9 months, 2 weeks ago
  3. Crispy Form by sourabhsinha396 10 months, 1 week ago
  4. ReadOnlySelect by mkoistinen 10 months, 2 weeks ago
  5. Verify events sent to your webhook endpoints by santos22 11 months, 2 weeks ago

Comments

Please login first before commenting.