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
MultipleObjectsReturned at /premium/1/ get() returned more than one Book -- it returned 2!
I don't know where to look for the error. Here is my example 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)
Traceback:
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)
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)
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)
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)
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)
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)
File "C:\Users\admin\thesis\blackink_website\catalog\views.py" in get 127. book = get_object_or_404(Book)
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)
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)
Exception Type: MultipleObjectsReturned at /premium/1/ Exception Value: get() returned more than one Book -- it returned 2!
I will update the post if the information is not enough. Thanks in advance.
UPDATE
here's my models.py
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)])
page_detail.html
{% if user_membership and user_membership.user == request.user %}
{% for content in book.pages %}
<a href="{{ content.get_absolute_url }}" class="site-btn">Read</a>
{% endfor %}
{% else %}
<button class="site-btn" disabled="disabled">VIP</button>
{% endif %}
Book
andPage
? Please post yourmodels.py
– Daniel Holmesget_object_or_404
expects to return a single object.book = get_object_or_404(Book)
is failing because there are two books in the database. Which book do you want to fetch? On the next line you havepage = get_object_or_404(Page)
- which page do you want to fetch? Finally, please show your URL patterns. – Alasdair