Answer the question
In order to leave comments, you need to log in
Custom serialization in django-rest-framework - how to customize attribute name with value?
For example, there are classes:
class Person(models.Model):
name = models.CharField()
....
class Document(models.Model):
person = models.ForeignKey(Person, related_name='docs', ..)
doc_type = models.CharField()
doc_number = models.CharField()
doc_seria = models.CharField()
...
{
"Person": {
"name": "Ivan Petrov",
"passport": {
"doc_numer": "123455",
"doc_seria": "1234"
},
"zagran" : {
"doc_number": "12543",
"doc_seria": 1234543
}
}
Answer the question
In order to leave comments, you need to log in
Since the response schema changes, it will not be possible to describe the list of fields through serializers.
Person = SerializerMethodField(method="get_person")
def get_person(obj):
return {"name": ..., "passport": ...}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question