[Answer]-Django TastyPie Patch to a Many-to-Many

1👍

You must use brackets such as

[] 

around your schemes items (even if singular) when posting to a m2m field.

Request would then look like :

{"email":"test@test.com",  "schemes":[{"id":"12", "schemes"}]}

When you want to know what a request should look like, make a GET request on url/of/api/modelresource/schema/

If I recall correctly (and although you wrote “POST” in your request), PATCH request must have

{"objects": [...]}

enclosing the body.

EDIT :

Here’s an example of what works for me :

Resources :

class VATCertificateResource(ModelResource):
    class Meta:
        queryset = VATCertificate.objects.all()
        resource_name = 'vatcertificate'
        authorization = Authorization()

class InterventionResource(ModelResource):

    vatcertificates = fields.ToManyField('core.api.VATCertificateResource', 'vatcertificates',
                                     related_name='intervention', null=True, blank=True, full=True)

Models :

class VATCertificate(Document):
    intervention = models.ForeignKey(Intervention, related_name='vatcertificates', blank=True, null=True)

class Intervention(models.Model):
    pass

Hope this helps,

Regards,

Leave a comment