Exemplo n.º 1
0
'''
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)
'''
Create Obj
'''
data = {'user': 1}
serializer = StatusSerializer(data=data)
serializer.is_valid()
serializer.save()

# if serializer.is_valid():
#     serializer.save()
'''
Update obj
'''
obj = Status.objects.first()
data = {'content': 'some new content', 'user': 1}
update_serializer = StatusSerializer(obj, data=data)
update_serializer.is_valid()
update_serializer.save()
'''
Delete Obj
'''
Exemplo n.º 2
0
serializer2 = StatusSerializer(qs, many=True)
serializer2.data
json_data2 = JSONRenderer().render(serializer2.data)
print(json_data2)
print(type(json_data2))

stream2 = BytesIO(json_data2)
data2 = JSONParser().parse(stream2)
print(data2)
type(data2)


# Create object
data = {'user':1}
serializer = StatusSerializer(data=data)
serializer.is_valid()
serializer.save()


# Update object
obj = Status.objects.first()

data = {"content":"update from serializer", "user":1}

update_serializer = StatusSerializer(obj, data=data)
update_serializer.is_valid()
update_serializer.save()



Exemplo n.º 3
0
'''
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
'''
obj = Status.objects.first()
data = {'content': 'some new content', "user": 1}
update_serializer = StatusSerializer(obj, data=data)
update_serializer.is_valid()
update_serializer.save()
'''
Delete obj
'''
Exemplo n.º 4
0
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 obj

data = {"user": 1}

serializer = StatusSerializer(data=data)

serializer.is_valid()
serializer.save()

# if serializer.is_valid():
#     serializer.save()
'''
Update obj
'''
obj = Status.objects.first()
data = {'content': 'Some new content'}
update_serializer = StatusSerializer(object=obj, data=data)  # returns object
update_serializer.is_valid()
update_serializer.save()
'''
Delete obj
'''
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)

stream = BytesIO(json_data)
print(stream)
data = JSONParser().parse(stream)
print(data)
#
# data = {'user': 1, 'content': 'New content'}
# serializer = StatusSerializer(data=data)
# serializer.is_valid()
# serializer.save()
#
obj = Status.objects.get(id=3)
print(obj)
data = {'content': 'New', 'user': 1}
serializer = StatusSerializer(instance=obj, data=data)
serializer.is_valid()
serializer.save()
Exemplo n.º 6
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)