I'm building a django tastypie api, and I have a problem with adding elements in ManyToMany
relationships
Example, models.py
class Picture(models.db):
""" A picture of people"""
people = models.ManyToManyField(Person, related_name='pictures',
help_text="The people in this picture",
)
class Person(models.db):
""" A model to represet a person """
name = models.CharField(max_length=200,
help_text="The name of this person",
)
resources:
class PictureResource(ModelResource):
""" API Resource for the Picture model """
people = fields.ToManyField(PersonResource, 'people', null=True,
related_name="pictures", help_text="The people in this picture",
)
class PersonResource(ModelResource):
""" API Resource for the Person model """
pictures = fields.ToManyField(PictureResource, 'pictures', null=True,
related_name="people", help_text="The pictures were this person appears",
)
My problem is that I would like to have an add_person
end point in my picture resource.
If I use PUT
, then I need to specify all the data in the picture
If I use PATCH
, I still need to specify all the people in the picture.
Of course I could simply generate the /api/picture/:id/add_people
URL and there I could handle my problem. The problem with that is that it does not feel clean.
Another solution would be to generate the /api/picture/:id/people
end point, and there I could do GET
, POST
, PUT
, like it's a new resource, but I don't know how to implement this and it seems strange to create new people under this resource.
Any thoughts?