title: Django add User and Listing model to a Watchlist model
body: <p>I'm trying to add a Listing model for an eCommerce app to a Wishlist model, but I'm not quite sure where I'm going wrong.</p>
  <p>I referenced this post in order to create a Wishlist model and link it to my User and Listing models via a Foreign key for each.
  <a href="https://stackoverflow.com/questions/63403309/watchlist-system-on-django">Wishlist post</a></p>
  <p>urls.py</p>
  <pre><code>from django.urls import path
  from . import views
  
  
  app_name = &quot;auctions&quot;
  urlpatterns = [
  
      # Listing paths
      path(&quot;&quot;, views.index, name=&quot;index&quot;),
      path(&quot;login/&quot;, views.login_view, name=&quot;login&quot;),
      path(&quot;logout/&quot;, views.logout_view, name=&quot;logout&quot;),
      path(&quot;register/&quot;, views.register, name=&quot;register&quot;),
      path(&quot;addlisting/&quot;, views.add_listing, name=&quot;add_listing&quot;),
      path(&quot;viewlisting/&lt;int:listing_id&gt;&quot;, views.view_listing, name=&quot;view_listing&quot;),
  
      # Watchlist paths
      path(&quot;watchlist_add/&lt;int:listing_id&gt;&quot;, views.watchlist_add, name=&quot;watchlist_add&quot;),
  
  ]
  </code></pre>
  <p>models.py</p>
  <pre><code>from django.contrib.auth.models import AbstractUser
  from django.db import models
  from datetime import datetime
  
  
  # Categories
  categories = [
      ('misc', 'misc'),
      ('computers','computers'),
      ('toys','toys'),
      ('pets','pets'),
      ('beauty','beauty'),
      ('video games','video games'),
      ('food','food'),
      ('clothing','clothing'),
  ]
  
  
  ACTIVE_CHOICES = [
      ('Yes', 'Yes'),
      ('No', 'No'),
  ]
  
  class User(AbstractUser):
      pass
  
  
  class Listing(models.Model):
      listing_owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name=&quot;owner&quot;)
  
      listing_title = models.CharField(max_length=100)
      listing_description = models.TextField(max_length=1200)
      listing_img = models.URLField(null=True, blank=True, max_length=250)
      created_on = models.DateTimeField(auto_now=True)
      starting_price = models.DecimalField(decimal_places=2, max_digits=10, default=1.00)
  
      # From above categories list
      category = models.CharField(max_length=30, choices=categories, default='misc')
      # From active or inactive list
      active_flag = models.CharField(max_length=10, choices=ACTIVE_CHOICES, default=&quot;Yes&quot;, verbose_name=&quot;active listing&quot;)
  
      class Meta:
          ordering = ['-created_on']
  
  
      def __str__(self):
          return f&quot;Name: {self.listing_title}, Price: ${self.starting_price}, Posted By: {self.listing_owner}&quot;
  
  
  class Watchlist(models.Model):
      user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
      item = models.ForeignKey(Listing, on_delete=models.CASCADE, null=True)
  
      def __str__(self):
          return f&quot;{self.item}&quot;
  </code></pre>
  <p>views.py</p>
  <pre><code>@login_required
  def view_listing(request, listing_id):
      listing = get_object_or_404(Listing, pk=listing_id)
      if request.method == &quot;GET&quot;:
          return render(request, &quot;auctions/view_listing.html&quot; , { &quot;listing&quot;: listing})
      else:
          try:
              form = ListingForm(request.POST, instance=listing)
              form.save()
              return redirect(&quot;auctions:index&quot;)
          except ValueError:
              return render(request, &quot;auctions/view_listing.html&quot;, {&quot;listing&quot;: listing, &quot;form&quot;: form})
  
  
  @login_required
  def watchlist_add(request, listing_id):
      listing = get_object_or_404(Listing, pk=listing_id)
      obj, created = Watchlist.objects.get_or_create(user=request.user, item_id=listing_id)
  
      return HttpResponseRedirect(reverse(&quot;auctions:index&quot;))
  </code></pre>
  <p>view_listing.html</p>
  <pre><code>{% extends &quot;auctions/layout.html&quot; %} {% block body %}
  &lt;h1 class=&quot;display-3 text-center mb-5&quot;&gt;{{ listing.listing_title }}&lt;/h1&gt;
  &lt;div class=&quot;container d-flex&quot;&gt;
      &lt;figure class=&quot;figure&quot;&gt;
          {% if listing.listing_img %}
          &lt;img src=&quot;{{ listing.listing_img }}&quot; alt=&quot;{{ listing.listing_title }}&quot; title=&quot;{{ listing.listing_title }}&quot;&gt; {% else %}
          &lt;img src=&quot;https://res.cloudinary.com/guez/image/upload/v1606759078/cs50_commerce_project/default_image.jpg&quot; alt=&quot;default image&quot; title=&quot;default image&quot;&gt; {% endif %}
          &lt;figcaption class=&quot;figure - caption&quot;&gt;Listed By: {{ listing.listing_owner|capfirst }}&lt;/figcaption&gt;
      &lt;/figure&gt;
  
  
      &lt;div class=&quot;container m-4&quot;&gt;
          &lt;p class=&quot;display-4&quot;&gt;${{ listing.starting_price }}&lt;/p&gt;
          &lt;hr&gt;
          &lt;p class=&quot;&quot;&gt;Description: {{ listing.listing_description }}&lt;/p&gt;
          &lt;p class=&quot;figure-caption&quot;&gt;Category: {{ listing.category|capfirst }}&lt;/p&gt;
          &lt;small&gt;&lt;/small&gt;
      &lt;/div&gt;
      &lt;div class=&quot;container&quot;&gt;
          &lt;form action=&quot;{% url 'auctions:watchlist_add' listing.pk %}&quot; method=&quot;post&quot;&gt;
              {% csrf_token %}
              &lt;input type=&quot;hidden&quot; value=&quot;{{ listing.pk }}&quot; name=&quot;listing&quot;&gt;
              &lt;input type=&quot;submit&quot; value=&quot;Add To Watchlist&quot; class=&quot;btn btn-outline-warning&quot;&gt;
          &lt;/form&gt;
          &lt;!--&lt;a href=&quot;{% url 'auctions:watchlist_add' listing.pk %}&quot; class=&quot;btn btn-outline-warning&quot;&gt;Add To Watchlist&lt;/a&gt;--&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  {% endblock %}
  
  </code></pre>
  <p>Before there are any items on my wishlist, I go to view_listing.html and hit the 'Add To Watchlist' button. I then get an AttributeError that reads &quot;AttributeError: 'Listing' object has no attribute 'remove'&quot;. Here's the stack trace:</p>
  <pre><code>System check identified no issues (0 silenced).
  December 07, 2020 - 00:08:42
  Django version 3.1, using settings 'commerce.settings'
  Starting development server at http://0.0.0.0:8080/
  Quit the server with CONTROL-C.
  [07/Dec/2020 00:08:44] &quot;GET /admin/auctions/watchlist/2/delete/ HTTP/1.1&quot; 200 5409
  [07/Dec/2020 00:08:45] &quot;POST /admin/auctions/watchlist/2/delete/ HTTP/1.1&quot; 302 0
  [07/Dec/2020 00:08:45] &quot;GET /admin/auctions/watchlist/ HTTP/1.1&quot; 200 5215
  [07/Dec/2020 00:08:46] &quot;GET /admin/jsi18n/ HTTP/1.1&quot; 200 3187
  [07/Dec/2020 00:08:48] &quot;GET /admin/auctions/watchlist/ HTTP/1.1&quot; 200 5215
  [07/Dec/2020 00:08:48] &quot;GET /admin/jsi18n/ HTTP/1.1&quot; 200 3187
  [07/Dec/2020 00:08:51] &quot;GET /admin/auctions/watchlist/ HTTP/1.1&quot; 200 5215
  [07/Dec/2020 00:08:51] &quot;GET /admin/jsi18n/ HTTP/1.1&quot; 200 3187
  [07/Dec/2020 00:09:01] &quot;GET / HTTP/1.1&quot; 200 3962
  [07/Dec/2020 00:09:03] &quot;GET /viewlisting/8 HTTP/1.1&quot; 200 2350
  Name: Sega Genesis, Price: $1.00, Posted By: guez already in cart.
  Internal Server Error: /watchlist_add/8
  Traceback (most recent call last):
    File &quot;/home/ubuntu/.local/lib/python3.7/site-packages/django/core/handlers/exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;/home/ubuntu/.local/lib/python3.7/site-packages/django/core/handlers/base.py&quot;, line 179, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;/home/ubuntu/.local/lib/python3.7/site-packages/django/contrib/auth/decorators.py&quot;, line 21, in _wrapped_view
      return view_func(request, *args, **kwargs)
    File &quot;/home/ubuntu/project2/commerce/auctions/views.py&quot;, line 108, in watchlist_add
      obj.listings.remove(listing)
  AttributeError: 'Listing' object has no attribute 'remove'
  [07/Dec/2020 00:09:06] &quot;POST /watchlist_add/8 HTTP/1.1&quot; 500 73233
  </code></pre>
  <p>I want to add the item to Wishlist via the <code>watchlist_add</code> function and then pass that new Wishlist object to the <code>view_listing</code> function in order to determine whether the button should or should not exist in the <code>view_listing.html</code> template if the item is already in <code>request.user </code>'s Watchlist.</p>
  <p>?<a href="https://i.stack.imgur.com/vYHtS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vYHtS.png" alt="Object added to wishlist" /></a></p>