Most efficient way to create new many to many relationship

95 views
Skip to first unread message

Tom Carr

unread,
Dec 2, 2021, 1:36:21 AM12/2/21
to Django REST framework

I am developing a web app where users can like something (let's say books). In my models I've created a many to many relationship between users and books. In the front end I've built in React, the users can click an image of a book, and this sends an API PATCH request to add a new "like" to the books class (this is the 'related' field). I can't see an easy way to add this like. I can send a PATCH request to update a "likes" field in the Books class (where a 'like' corresponds to a User pk), but if a new PATCH request is sent from a later like it overwrites instead of appends.

Is the only answer to write some sort of custom PATCH function so likes append rather than overwrite or is there something I am missing in the documentation as it seems like a fairly simple use case. I've tried using a through table, but then I can't seem to POST a request to that table.

Many thanks

Reply all
Reply to author
Forward
0 new messages