title: Django class view - MultipleObjectsReturned error
body: <p>I'm still new at django so please bear with me. I'm trying to make a website about books. Now I'm having error like this</p>
  
  <blockquote>
    <p>MultipleObjectsReturned at /premium/1/
    get() returned more than one Book -- it returned 2!</p>
  </blockquote>
  
  <p>I don't know where to look for the error. Here is my example code.</p>
  
  <pre><code>class PageDetailView(LoginRequiredMixin, generic.View):
  
      def get(self, request, *args, **kwargs):
          book = get_object_or_404(Book)
          page = get_object_or_404(Page)
          user_membership = get_object_or_404(Customer, user=request.user)
          user_membership_type = user_membership.membership.membership_type
          user_allowed = book.allowedMembership.all()
          context = {'object': None}
          if user_allowed.filter(membership_type=user_membership_type).exists():
              context = {'object': page}
          return render(request, "catalog/page_detail.html", context)
  </code></pre>
  
  <blockquote>
    <p>Traceback:</p>
    
    <p>File
    "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py"
    in inner
      34.             response = get_response(request)</p>
    
    <p>File
    "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py"
    in _get_response
      126.                 response = self.process_exception_by_middleware(e, request)</p>
    
    <p>File
    "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py"
    in _get_response
      124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)</p>
    
    <p>File
    "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\base.py"
    in view
      68.             return self.dispatch(request, *args, **kwargs)</p>
    
    <p>File
    "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\mixins.py"
    in dispatch
      52.         return super().dispatch(request, *args, **kwargs)</p>
    
    <p>File
    "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\base.py"
    in dispatch
      88.         return handler(request, *args, **kwargs)</p>
    
    <p>File "C:\Users\admin\thesis\blackink_website\catalog\views.py" in get
      127.         book = get_object_or_404(Book)</p>
    
    <p>File
    "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\shortcuts.py"
    in get_object_or_404
      93.         return queryset.get(*args, **kwargs)</p>
    
    <p>File
    "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py"
    in get
      403.             (self.model._meta.object_name, num)</p>
    
    <p>Exception Type: MultipleObjectsReturned at /premium/1/ Exception
    Value: get() returned more than one Book -- it returned 2!</p>
  </blockquote>
  
  <p>I will update the post if the information is not enough. Thanks in advance.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>here's my models.py</p>
  
  <pre><code>class Book(models.Model):
  
  
  title = models.CharField(max_length=200) #i deleted some info to make it shorter
  
  allowedMembership = models.ManyToManyField(Membership, blank=True)
  
  
  def get_absolute_url(self):
      return reverse('book-detail',  args=[str(self.id)])
  
  def __str__(self):
      return self.title
  
  @property
  def pages(self):
      return self.page_set.all()
  
  
  
  class Page(models.Model):
  book = models.ForeignKey(Book, on_delete=models.CASCADE, null=True)
  file = models.FileField(upload_to='book_content', validators=[pdf_file_extension], help_text="PDF File Only")
  timestamp = models.DateTimeField(default=timezone.now)
  
  
  def __str__(self):
      return self.book.title
  
  def get_absolute_url(self):
      return reverse('page-detail',   args=[str(self.id)])
  </code></pre>
  
  <p>page_detail.html</p>
  
  <pre><code>{% if user_membership and user_membership.user == request.user %}
          {% for content in book.pages %}
              &lt;a href="{{ content.get_absolute_url }}" class="site-btn"&gt;Read&lt;/a&gt;
          {% endfor %}
          {% else %}
          &lt;button class="site-btn" disabled="disabled"&gt;VIP&lt;/button&gt;
          {% endif %}
  </code></pre>