I am trying to store a User's ID and the ID of a Listing in a table. I am new to web development and to me, this seems like a good time to use a ManyToManyField:
class Watchlist(models.Model):
id = models.AutoField(primary_key=True)
user = models.ManyToManyField(User)
listing = models.ManyToManyField(Listing)
When I try to save a new entry in the database, by doing this:
listing_id = self.kwargs['pk']
item = Listing.objects.get(id=listing_id)
user_object = self.request.user
add_to_watchlist = Watchlist(user = user_object, listing = item)
add_to_watchlist.save()
I get the error:
TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use user.set() instead.
I am not sure what I am doing wrong, I have followed the example in the documentation as much as possible.