I have developed an API using django-rest-framework.
I am using ModelSerializer to return data of a model.
models.py
class MetaTags(models.Model):
title = models.CharField(_('Title'), max_length=255, blank=True, null=True)
name = models.CharField(_('Name'), max_length=255, blank=True, null=True)
serializer.py
class MetaTagsSerializer(serializers.ModelSerializer):
class Meta:
model = MetaTags
response
{
"meta": {
"title": null,
"name": "XYZ"
}
}
Ideally in an API response any value which is not present should not be sent in the response.
When the title
is null
I want the response to be:
{
"meta": {
"name": "XYZ"
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…