'django endpoint not returning all fields specified in serializer
This is Source Def:
class SourceDefinition(models.Model):
source = models.ForeignKey(Source, on_delete=models.DO_NOTHING)
special_id = models.IntegerField(default=0)
ad_group = models.CharField(max_length=50)
creator = models.CharField(max_length=100)
config = JSONField(default=dict, blank=True)
class SourceDefinitionSerializer(serializers.ModelSerializer):
source = SourceSerializer(read_only=True, many=False)
source_id = serializers.PrimaryKeyRelatedField(source="source", queryset=Source.objects.all(),
write_only=True)
class Meta:
model = SourceDefinition
fields = ['id', 'source', 'source_id', 'special_id', 'ad_group', 'creator', 'config']
Engine:
class EngineDefinition(models.Model):
engine = models.ForeignKey(Engine, on_delete=models.DO_NOTHING)
source_def = models.ForeignKey(SourceDefinition, on_delete=models.DO_NOTHING)
schedule_def = models.ForeignKey(ScheduleDefinition, on_delete=models.DO_NOTHING, null=True, blank=True)
ad_group = models.CharField(max_length=50)
creator = models.CharField(max_length=100)
where_clause = models.CharField(max_length=1000, null=True, blank=True)
config = JSONField(default=dict, blank=True)
class EngineDefinitionSerializer(serializers.ModelSerializer):
engine = EngineSerializer(read_only=True, many=False)
source_def = ScheduleDefinitionSerializer(read_only=True, many=False)
schedule_def = SourceDefinitionSerializer(read_only=True, many=False)
engine_id = serializers.PrimaryKeyRelatedField(source="engine", queryset=Engine.objects.all(),
write_only=True)
source_def_id = serializers.PrimaryKeyRelatedField(source="source_def", queryset=SourceDefinition.objects.all(),
write_only=True)
schedule_def_id = serializers.PrimaryKeyRelatedField(source="schedule_def", queryset=ScheduleDefinition.objects.all(),
write_only=True)
class Meta:
model = EngineDefinition
fields = ['id', 'engine', 'source_def', 'schedule_def', 'engine_id', 'source_def_id', 'schedule_def_id',
'ad_group', 'creator', 'where_clause', 'config']
Returned value from endpoint using EngineDefinitionSerializer
:
{
"id": 1,
"engine": {
"id": 1,
"name": "en"
},
"source_def": {
"id": 1,
"ad_group": "YYY",
"creator": "EEE",
"config": {}
},
"schedule_def": null,
"ad_group": "YYY",
"creator": "EEE",
"where_clause": null,
"config": {}
}
Why is special_id
not being returned in the EngineDefinitionSerializer
endpoint response? I also attempted to specify all field names instead of using __all__
and same result occurred.
...........
(need more words words worcs words words words wrods)
Solution 1:[1]
If you have a custom filed in you serializer, like source and source_id in your case, you must specify it explicitly in the fields list. Specify all fileds one by one, plus source and source_id too.
Example, in your case:
class SourceDefinitionSerializer(serializers.ModelSerializer):
source = SourceSerializer(read_only=True, many=False)
source_id = serializers.PrimaryKeyRelatedField(source="source", queryset=Source.objects.all(), write_only=True)
class Meta:
model = SourceDefinition
fields = '[source, source_id, filed1, field2, .......]'
Regretfully there is no other way to use __all__
somehow with custom fields right now as of July 2020, this is the source code that parses fileds
clause of Meta
class, you can check out how it works, not too compilcated code
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |