Reputation: 302
I am using django-rest-framework
- I want to rename (in list) the field in model serializer and to update with same name which I renamed in list (to update).
class ConfiglistSerializer(serializers.ModelSerializer):
class Meta:
model = Config
fields = ('id', 'configname', 'mac_address')
def to_representation(self, obj):
return {
'id': obj.id,
'configname': obj.name,
'macAddress': obj.mac_address
}
How to update with the name ("configname" , "macAddress")
in put
or patch
?
Upvotes: 2
Views: 1749
Reputation: 4067
No need to do anything on update. Just define fields with names , that you want by using the serializer fields
and specifying source param. The rest-framwork serilizer will do other parts.
For ex. macAddress = serializers.CharField(source='mac_address')
See here http://www.django-rest-framework.org/api-guide/fields/ to how serializer fields works
Upvotes: 0
Reputation: 291
You have to use parser and renderer for camel case:
$ pip install djangorestframework-camel-case
And to add the render and parser to your django settings file.
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'djangorestframework_camel_case.render.CamelCaseJSONRenderer',
# Any other renders
),
'DEFAULT_PARSER_CLASSES': (
'djangorestframework_camel_case.parser.CamelCaseJSONParser',
# Any other parsers
),
}
That way you will be able to work with snake_case in your serializer, and camelCase in API without custom to_representation
method
Upvotes: 2