Beispiel #1
0
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.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 = JSONRender().render(serializer2.data)
print(json_data2)

stream2 = BytesIO(json_data2)
data2 = JSONParse().parse(stream2)
print(data2)
'''
Beispiel #2
0
from django.utils.six import BytesIO
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser

from status.api.serializers import StatusSerializer, CustomSerializer
from status.models import Status
'''
Serialize a single object
'''
obj = Status.objects.first()
serializer = StatusSerializer(obj)
serializer.data
json_data = JSONRenderer().render(serializer.data)
print(json_data)

stream = BytestIO(json_data)
data = JSONParser().parse(stream)
print(data)
'''
Create obj
'''
data = {'user': 1}
serializer = StatusSerializer(data=data)
serializer.is_valid()
serializer.save()

if serializer.is_valid():
    serializer.save()
'''
Update obj
'''
from six import BytesIO #-----> install pip six & pip install --upgrade django-cors-headers
# from django.utils.six import BytesIO ---> this was removed from django 3.0 refer to -->https://stackoverflow.com/questions/59193514/importerror-cannot-import-name-six-from-django-utils
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from status.models import Status
from status.api.serializers import StatusSerializer

# Serialize a single object

obj = Status.objects.first()
serializer = StatusSerializer(obj)
serializer.data

	"""Converting Serialized data into JSON"""
json_data = JSONRenderer().render(serializer.data)
print(json_data)
print(type(json_data))
		
		""" conversion of JSON ---> List object """
stream = BytesIO(json_data)
data = JSONParser().parse(stream)
print(data)
type(data)


# Serialize a queryset 

qs = Status.objects.all()
serializer2 = StatusSerializer(qs, many=True)
serializer2.data
json_data2 = JSONRenderer().render(serializer2.data)
Beispiel #4
0
 def post(self, request, format=None):
     qs = Status.objects.all()
     serializer = StatusSerializer(qs, many=True)
     return Response(serializer.data)
Beispiel #5
0
"""
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)
Beispiel #6
0
# 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)
Beispiel #8
0
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)