Home > Software design >  A Django model that only has many to many fields?
A Django model that only has many to many fields?

Time:06-08

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.

CodePudding user response:

You can not directly assign a value to a ManyToManyField, but use the .add(…) method [Djang-doc] as the error indicates:

from django.shortcuts import get_object_or_404

item = get_object_or_404(Listing, pk=self.kwargs['pk'])

watchlist = Watchlist.objects.create()
watchlist.user.add(request.user)
watchlist.listing.add(item)

Note: It is often better to use get_object_or_404(…) [Django-doc], then to use .get(…) [Django-doc] directly. In case the object does not exists, for example because the user altered the URL themselves, the get_object_or_404(…) will result in returning a HTTP 404 Not Found response, whereas using .get(…) will result in a HTTP 500 Server Error.


Note: Since a ManyToManyField refers to a collection of elements, ManyToManyFields are normally given a plural name. You thus might want to consider renaming user to users.

  • Related