0๐
โ
Not sure your model looks like, but I think you just need to add SerializerMethodField on your CodeClientSerializer.
class CodeClientSerializer(serializers.ModelSerializer):
hours = serializers.SerializerMethodField()
min_hours = serializers.SerializerMethodField()
class Meta:
model = CodeClient
fields = (
...
'hours',
'min_hours',
)
def get_hours(self, obj):
return HOURS # set your value here
def get_min_hours(self, obj):
return MIN_HOURS # set your value here
๐คgzerone
1๐
I need to create API REST base on models of the project
DRF provides a ModelViewSet
just for that, all you do is tie it to one of your models
class MyModelViewSet(viewsets.ModelViewSet):
"""
A viewset for viewing and editing user instances.
"""
serializer_class = MyModelSerializer
queryset = MyModel.objects.all()
http://www.django-rest-framework.org/api-guide/viewsets/#modelviewset
I need to use nested serializers
and for your serializer you can do:
class MyModelSerializer(serializers.ModelSerializer):
nested_field = MyOtherModelSerializer()
class Meta:
model = MyModel
fields = ('field1', 'field2', 'field3')
http://www.django-rest-framework.org/api-guide/serializers/#specifying-nested-serialization
๐คbakkal
Source:stackexchange.com