lundi 26 novembre 2018

Exclude nested model field in django serializer

So I have two django models but one of them has a field I need to exclude called code. I haven't found a way to exclude this field using the serializer class.

class ModelA(models.Model):
    modelBs = models.ManyToManyField('ModelB')

class ModelB(models.Model):
    # assume all sorts of fields here like name, location, etc..
    code = models.CharField(max_length=200)

My issue is I can't exclude the modelB code when serializing model A like so:

class ModelASerializer(serializers.ModelSerializer):
    class Meta:
        model = ModelA
        exclude = ('modelBs__code')

The serializer above does not work. Anyone know how to go about this?




Aucun commentaire:

Enregistrer un commentaire