-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathserializers.py
More file actions
55 lines (46 loc) · 1.54 KB
/
serializers.py
File metadata and controls
55 lines (46 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from rest_framework import serializers
from .models import Todo
from django.contrib.auth.models import User
"""
TODO:
Create the appropriate Serializer class(es) for implementing
Todo GET (List and Detail), PUT, PATCH and DELETE.
"""
class TodoCreateSerializer(serializers.ModelSerializer):
"""
TODO:
Currently, the /todo/create/ endpoint returns only 200 status code,
after successful Todo creation.
Modify the below code (if required), so that this endpoint would
also return the serialized Todo data (id etc.), alongwith 200 status code.
"""
def save(self, **kwargs):
data = self.validated_data
user = self.context['request'].user
title = data['title']
todo = Todo.objects.create(creator=user, title=title)
response = {
"id": todo.id,
"title": todo.title,
}
return response
class Meta:
model = Todo
fields = ('id', 'title',)
class TodoCommonSerializer(serializers.ModelSerializer):
class Meta:
model = Todo
fields = ('id', 'title',)
class TodoListSerializer(serializers.ModelSerializer):
class Meta:
model = Todo
fields = ('id', 'title', 'collaborators',)
class UserCollabSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username',)
class TodoCollabSerializer(serializers.ModelSerializer):
collaborators = UserCollabSerializer(read_only=True, many=True)
class Meta:
model = Todo
fields = ('id', 'collaborators',)