-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathserializers.py
More file actions
27 lines (19 loc) · 767 Bytes
/
serializers.py
File metadata and controls
27 lines (19 loc) · 767 Bytes
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
from django.utils.timezone import now
from rest_framework import serializers
from musics.models import Music
class ToUpperCaseCharField(serializers.CharField):
def to_representation(self, value):
return value.upper()
class MusicSerializer(serializers.ModelSerializer):
days_since_created = serializers.SerializerMethodField()
singer = ToUpperCaseCharField()
class Meta:
model = Music
# fields = '__all__'
fields = ('id', 'song', 'singer', 'last_modify_date', 'created', 'days_since_created')
def get_days_since_created(self, obj):
return (now() - obj.created).days
class MusicSerializerV1(serializers.ModelSerializer):
class Meta:
model = Music
fields = ('id', 'song', 'singer')