from django.utils.six import BytesIO from rest_framework.renderers import JSONRenderer from rest_framework.parsers import JSONParser from status.api.serializers import StatusSerializer from status.models import Status #serialize a single object obj = Status.object.first() serializer = StatusSerializer(obj) serializer.data json_data = JSONRenderer().render(serializer.data) print(json_data) stream = BytesIO(json_data) data = JSONParser().parse(stream) print(data) #serialize a queryset qs = Status.object.all() serializer2 = StatusSerializer(qs, many=True) serializer2.data json_data2 = JSONRenderer().render(serializer2.data) print(json_data2) stream2 = BytesIO(json_data2) data2 = JSONParser().parse(stream2) print(data2) #create object data = {'user': 1} create_serializer = StatusSerializer(data=data)
from django.utils.six import BytesIO from rest_framework.renderers import JSONRenderer from rest_framework.parsers import JSONParser from status.api.serializers import StatusSerializer from status.api.serializers import CustomSerializer from status.models import Status ''' Serialize a single objects ''' obj = Status.objects.first() serializer = StatusSerializer(obj) serializer.data json_data = JSONRenderer().render(serializer.data) print(json_data) stream = BytesIO(json_data) data = JSONParser().parse(stream) print(data) ''' Serialize a queryset ''' qs = Status.objects.all() serializer2 = StatusSerializer(qs, many=True) serializer2.data json_data2 = JSONRenderer().render(serializer2.data) print(json_data2)
""" Just Notes """ from django.utils.six import BytesIO from rest_framework.parsers import JSONParser from rest_framework.renderers import JSONRenderer from status.api.serializers import StatusSerializer from status.models import Status obj = Status.objects.first() serializer = StatusSerializer(obj) print(serializer.data) json_data = JSONRenderer().render(serializer.data) print(json_data) stream = BytesIO(json_data) data = JSONParser().parse(stream) print(data) """ serialize a queryset """ qs = Status.objects.all() serializer2 = StatusSerializer(qs, many=True) serializer2.data json_data2 = JSONRenderer().render(serializer2.data) print(json_data2) stream2 = BytesIO(json_data2)
# Serializing using Shell: import json from rest_framework.renderers import JSONRenderer from django.utils.six import BytesIO from rest_framework.parsers import JSONParser from status.models import Status from status.api.serializers import StatusSerializer ''' serializing a single object. ''' qs = Status.object.first() serializer = StatusSerializer(qs) print(serializer.data) # gives ordered dict data from our model json_data = JSONRenderer().render(serializer.data) print(json_data) # gives byte format of JSON. or Called JSON # To convert back to Python data: json.loads(json_data) # will give list or dict data in python # When we have streaming data: stream = BytesIO(json_data) data = JSONParser().parse(stream) print(data) # will give list or dict data in python. ''' Serializing a queryset ''' qs = Status.object.first() serializer = StatusSerializer(qs, many=True) print(serializer.data) # gives ordered dict data from our model
from rest_framework.parsers import JSONParser from status.api.serializers import StatusSerializer from status.models import Status # get_user_model().objects.create_user( # username='******', # email='*****@*****.**', # password='******' # ) # data = { # 'user': 1, # 'content': 'Content' # } obj = Status.objects.first() serializer = StatusSerializer(obj) print(serializer.data) json_data = JSONRenderer().render(serializer.data) print(json_data) stream = BytesIO(json_data) print(stream) data = JSONParser().parse(stream) print(data) # print('-------------------------------') qs = Status.objects.all() serializer = StatusSerializer(qs, many=True) print(serializer.data) json_data = JSONRenderer().render(serializer.data) print(json_data)
from io import BytesIO from rest_framework.renderers import JSONRenderer from rest_framework.parsers import JSONParser from status.api.serializers import StatusSerializer from status.models import Status ''' Serialize a single object ''' obj = Status.objects.first() serializer = StatusSerializer(obj) json_data = JSONRenderer().render(serializer.data) print(json_data) #Second method stream = BytesIO(json_data) data = JSONParser().parse(stream) print(data) ''' Serialize a queryset ''' qs = Status.objects.all() serializer2 = StatusSerializer(qs, many=True) json_data2 = JSONRenderer().render(serializer2.data) print(json_data2) stream2 = BytesIO(json_data2) data2 = JSONParser().parse(stream2) print(data2) ''' create obj
from rest_framework.renderers import JSONRenderer from status.models import Status from status.api.serializers import StatusSerializer, CustomSerializer '''Serialize a Single Object''' obj = Status.objects.first() serializer = StatusSerializer(obj) serializer.data json_data = JSONRenderer().render(serializer.data) print(json_data) '''Serialize a Query Set''' qs = Status.objects.all() serializer2 = StatusSerializer(qs, many=True) serializer2.data json_data2 = JSONRenderer().render(serializer2.data) print(json_data2) '''Create obj''' data = {'user': 1} serializer = StatusSerializer(data=data) serializer.is_valid() if serializer.is_valid(): serializer.save() '''Update obj''' obj = Status.objects.first() # grab the obj to update data = {'content': 'some new content', 'user': 1} # data to update update_serializer = StatusSerializer(obj, data=data) update_serializer.is_valid() if update_serializer.is_valid(): update_serializer.save()
from status.api.serializers import StatusSerializer from status.models import Status # new record in database using serializer data = {'user': 1, 'content': 'Some content'} serializer = StatusSerializer(data=data) if serializer.is_valid(): serializer.save() # update reacord in database using serializer obj = Status.objects.last() data = {'user': 1, 'content': 'Some new content'} update_serializer = StatusSerializer(instance=obj, data=data) if update_serializer.is_valid(): update_serializer.save() # serializer jako walidator from rest_framework import serializers class CustomSerializer(serializers.Serializer): content = serializers.CharField() email = serializers.EmailField() sent_data = {"content": "ble ble ble", "email": "*****@*****.**"} create_serializer = CustomSerializer(data=sent_data) if create_serializer.is_valid(): new_data = create_serializer.data print(new_data)