Nested fields when using Meta.fields = '__all__' are not detected?
gonzaloamadio opened this issue · comments
Hi! , I have two serializers, one nested into the other. If I use the flag all in the Meta.fields in the nested serializer. I have the following error:
The field 'sample_groups' was declared on serializer HoldbackGroupSerializer, but has not been included in the 'fields' option.
Here is the code of the serializers
class HoldbackSerializer(DynamicFieldsMixin, NestedModelSerializer):
holdback_groups = NestedField(HoldbackGroupSerializer, many=True, required=False, create_ops=["create"])
class Meta:
model = Holdback
exclude = ('start_calendar_id',)
class HoldbackGroupSerializer(DynamicFieldsMixin, NestedModelSerializer):
# Many is true, because this is the reversed relation.
# Field is called as related name in SampleGroup FK field to this model.
sample_groups = NestedField(SampleGroupSerializer, many=True, required=False, create_ops=["create"])
class Meta:
model = HoldbackGroup
fields = '__all__'
Here are the models:
class Holdback(models.Model):
title = models.CharField(max_length=CHAR_FIELD_MAX_WIDTH)
class HoldbackGroup(models.Model):
name = models.CharField(max_length=128)
holdback = models.ForeignKey(
'Holdback', on_delete=models.PROTECT, db_column='holdback_id', related_name='holdback_groups'
)
class SampleGroup(models.Model):
name = models.CharField(max_length=128)
holdback_group = models.ForeignKey(
'HoldbackGroup', on_delete=models.PROTECT, db_column='holdback_group_id', related_name='sample_groups'
)
If I try to create a HoldBackGroup
posting to it's endpoint, it works ok (I post some other fields without sublocations).
But when I try to create a Holdback with a nested HoldbackGroup I have that error.
I am posting this payload:
{
"start_datetime": "2021-05-14T16:56:28.789002-03:00",
"holdback_groups": {
"create": [
{
"name": "somename3",
"holdback": 1
}
]
}
}
And having this error:
The field 'sample_groups' was declared on serializer HoldbackGroupSerializer, but has not been included in the 'fields' option.
Can I see how the sample_groups
field is defined in your serializer?. Including HoldbackGroupSerializer
will be helpful cuz it's the one that has been included here NestedField(HoldbackGroupSerializer, many=True, required=False, create_ops=["create"])
and not GroupSerializer
.
Can I see how the
sample_groups
field is defined in your serializer?. IncludingHoldbackGroupSerializer
will be helpful cuz it's the one that has been included hereNestedField(HoldbackGroupSerializer, many=True, required=False, create_ops=["create"])
and notGroupSerializer
.
Sorry, there you have more and better code. This is how it is
I think you might have a problem somewhere else in your code, I've copied your example as it is and tested it on my side but I've not encountered that error, I even copied your payload and this is what I got on my side after running it
{'id': 1, 'holdback_groups': [OrderedDict([('id', 1), ('sample_groups', []), ('name', 'somename3'), ('holdback', 1)])], 'title': 'Hello there'}
Below is the payload that I sent
{
"title": "Hello there",
"holdback_groups": {
"create": [
{
"name": "somename3"
}
]
}
}
I added the title because it was required.
I even tried including sample groups with the payload below
{
"title": "Hello there",
"holdback_groups": {
"create": [
{
"name": "somename3",
"sample_groups": {
"create": [{"name": "This is sample group"}]
}
}
]
}
}
And below is the response that I got
{'id': 1, 'holdback_groups': [OrderedDict([('id', 1), ('sample_groups', [OrderedDict([('id', 1), ('name', 'This is sample group'), ('holdback_group', 1)])]), ('name', 'somename3'), ('holdback', 1)])], 'title': 'Hello there'}