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.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()




# Delete object
Exemplo n.º 3
0
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_data)

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

# if serializer.is_valid():
#   serializer.save()

# Update Object
obj = Status.objects.first()
data = {'content': 'some new content', "user": 1}
update_serializer = StatusSerializer(obj, data=data)
serializer.is_valid()
serializer.save()

# Delete Object
# We'll first create an object before we delete it
data = {'user': 1, 'content': "Please delete me"}
create_obj_serializer = StatusSerializer(data=data)
data = JSONParser().parse(stream)
print(data)
"""Serialize queryset"""
qs = Status.objects.all()
serializer = StatusSerializer(qs, many=True)
json_data = JSONRenderer().render(serializer.data)
print(json_data)

stream = BytesIO(json_data)
data = JSONParser().parse(stream)
print(data)
"""Create object"""
data = {'user': 1}
serializer = StatusSerializer(data=data)
serializer.is_valid()
serializer.save()
"""Update object"""
obj = Status.objects.first()
data = {'content': 'Some new content', 'user': 1}
update_serializer = StatusSerializer(obj, data=data)
if update_serializer.is_valid():
    update_serializer.save()
"""Delete object"""
data = {'user': 1, 'content': 'Will be deleted now'}
create_obj_serializer = StatusSerializer(data=data)
create_obj_serializer.is_valid()
created_obj = create_obj_serializer.save()  # Instance of obj
print(created_obj)

obj = Status.objects.last()
obj.delete()
Exemplo n.º 5
0
qs = Status.objects.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 obj
"""
data = {'user': 1}
serializer = StatusSerializer(data=data)
serializer.is_valid()
serializer.save()  # You can't save it before running `.is_valid()`

Status.objects.count()
Status.objects.all()
"""
Create obj
"""
obj = Status.objects.first()
data = {'user': 1, 'content': 'some new content'}
update_serializer = StatusSerializer(obj, data=data)
update_serializer.is_valid()
update_serializer.save()
"""
Delete obj
"""
data = {'user': 1, 'content': 'please delete me'}
Exemplo n.º 6
0
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
'''
Exemplo n.º 7
0
#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)
create_serializer.is_valid()
create_serializer.save()

#update object
obj = Status.object.first()
data = {'user': 1, 'content': 'Some updated content'}
update_serializer = StatusSerializer(obj, data=data)
update_serializer.is_valid()
update_serializer.save()

#delete object
data = {'user': 1, 'content': 'please delete me'}
create_serializer = StatusSerializer(data=data)
create_serializer.is_valid()
created_obj = create_serializer.save()
print(created_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.º 9
0
serializer2.@staticmethod
json_data2 = JSONRenderer().render(serializer2.data)
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', "user": 1}
update_serializer = StatusSerializer(obj, data=data)
update_serializer.is_valid()
update_serializer.save()


Exemplo n.º 10
0
qs = Status.objects.all()
serializer2 = StatusSerializer(qs, many=True)
serializer2.data
json_data2 = JSONRenderer().render(serializer2.data)
print(json_data)

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', "user": 1}
update_serializer = StatusSerializer(obj, data=data)
update_serializer.is_valid()
update_serializer.save()
# update_serializer.errors <-- will show errors in shell
'''
Delete obj
'''
Exemplo n.º 11
0
'''
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
'''
data = {'user': 1}
serializer = StatusSerializer(data=data)
serializer.is_valid()
serializer.save()  #Need to call is valid before saving
'''
update obj
'''
obj = Status.objects.first()  #How to get the specific object
data = {'content': 'some new content', 'user': 1}
update_serializer = StatusSerializer(obj, data=data)
update_serializer.is_valid()
update_serializer.save()
'''
Delete obj
'''
data = {'user': 1, 'content': 'Please delete me'}
create_obj_serializer = StatusSerializer(data=data)
create_obj_serializer.is_valid()
create_obj = create_obj_serializer.save()
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()
'''Delete obj'''
data = {'user': 1, 'content': 'Please delete me'}
create_obj_serializer = StatusSerializer(data=data)
create_obj_serializer.is_valid()
create_obj = create_obj_serializer.save()  # returns instance of saved obj
print(create_obj)
print(create_obj.id)