Hi all,
I have a model which has a 'report' field. This is an HStoreField, meaning
that it expects key,value pairs. Through the api, an array is submitted. I
have a function, translate_code, which takes each element of that array and
creates a value for it, it should then save the key (code) and the value
(output of translate_code), as part of the HStoreField. I'm trying to
achieve this like so:
class ReportSerializer(serializers.ModelSerializer):
class Meta:
model = Report
fields = "__all__"
def create(self, validated_data):
codes = validated_data.pop("report")
report = {code: translate_code(code) for code in codes}
return Report(**validated_data, report=report)
However, the serializer raises a validation error because it expects a
"dict", not a "list". This is presumably because the validation is called
before the create function. I've tried writing a function called
"validate_report", hoping that this would override validation for the
report field, but it is not called. I've also tried adding validators = []
to the Serializer's Meta class, and writing a 'validate' function, as
suggested by the docs, but this is not called either. It seems as though it
calls the model's validation, but I thought that serializer's did their own
validation, rather than calling their model's clean function?
Thanks in advance for your help!
Alex
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.