Provide many, many fields with the Django REST Framework?

I am creating the following event system / signup using the Django REST Framework and cannot figure out how to configure it correctly.

In my event model, I have:

followers = models.ManyToManyField(get_user_model(), related_name='following')

Ideally, an authenticated user can use POST or PATCH to add or remove followers from a record for a given event. Although I'm not quite sure what the best way to do this.

My current thinking was to create a serializer that provides only a follower field, and then create an APIView using this serializer with the login in the get and post / patch methods to add or remove a specific user.

I get the feeling that this makes the situation too complicated. Is there an easier way to do this?

+3
source share
1 answer

What do you think of using an end-to-end model for the M2M relationship?

I mean:

class Follower(...:
  user = FK user
  event = FK event

...
followers = models.ManyToManyField(get_user_model(), through=Follower ...)
...

In this case, you can quickly create a model serializer and a general view for the Follower model. To add or remove a user to an event, you simply send POST or DELETE requests to this resource

+2
source

All Articles