Note that there are some explanatory texts on larger screens.

plurals
  1. POTastypie, add element to a many to many relationship
    text
    copied!<p>I'm building a django tastypie api, and I have a problem with adding elements in <code>ManyToMany</code> relationships</p> <p>Example, models.py</p> <pre><code>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", ) </code></pre> <p>resources:</p> <pre><code>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", ) </code></pre> <p>My problem is that I would like to have an <code>add_person</code> end point in my picture resource. If I use <code>PUT</code>, then I need to specify all the data in the picture If I use <code>PATCH</code>, I still need to specify all the people in the picture. Of course I could simply generate the <code>/api/picture/:id/add_people</code> URL and there I could handle my problem. The problem with that is that it does not feel clean.</p> <p>Another solution would be to generate the <code>/api/picture/:id/people</code> end point, and there I could do <code>GET</code>, <code>POST</code>, <code>PUT</code>, 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.</p> <p>Any thoughts?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload