Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested fields when using Meta.fields = '__all__' are not detected? #268

Open
gonzaloamadio opened this issue Aug 17, 2021 · 3 comments
Open

Comments

@gonzaloamadio
Copy link

gonzaloamadio commented Aug 17, 2021

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.

@yezyilomo
Copy link
Owner

yezyilomo commented Aug 17, 2021

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.

@gonzaloamadio
Copy link
Author

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.

Sorry, there you have more and better code. This is how it is

@yezyilomo
Copy link
Owner

yezyilomo commented Aug 19, 2021

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'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants