MultipleObjectsReturned Django
MultipleObjectsReturned Django
Im trying to display a page that shows all the bookings in a Booking model on a page.
views.py
def bookings(request):
booking_list = get_object_or_404(Booking.objects.filter().order_by("-day"))
return render(request, 'roombooker/base.html', 'booking_list': booking_list)
models.py
class Booking(models.Model):
day = models.DateField(u'Booking Day',help_text=u'Day of Booking')
start_time = models.TimeField(u'Start Time', help_text=u'Start Time')
end_time = models.TimeField(u'End Time', help_text=u'End Time')
user = models.ForeignKey('User', on_delete=models.SET_NULL,null=True)
room = models.ForeignKey('Room', on_delete=models.SET_NULL,null=True)
urls.py
urlpatterns =[
url(r'^bookings/',views.bookings, name='bookings'),
]
There are currently 10 dummy entries in the db that I put in.
When I try to go to the bookings page however I get
MultipleObjectsReturned at /bookings/
get() returned more than one Booking -- it returned 10!
Which is what I want, I wanted 10 Booking objects. The idea was to pass it to the html for rendering.
How can I solve this error please?
Thanks
get_list_or_404
get_object_or_404
.get()
1 Answer
1
Like the name get_object_or_404 suggests, this is used to retrieve a single element. Behind the curtains, it calls .get() on the queryset, and in case there is no element, it raises an exception, or a specified by the documentation:
get_object_or_404
.get()
Calls get() on a given model manager, but it raises Http404 instead of the model's DoesNotExist exception.
get()
Http404
DoesNotExist
get_object_or_404 however has a slibing that returns a collection of elements: get_list_or_404 [Django-doc]. The difference is that here it raises an exception if the set is empty, and we obtain a collection of elements. Or like specified by the documentation:
get_object_or_404
get_list_or_404
Returns the result of filter() on a given model manager cast to a list, raising Http404 if the resulting list is empty.
filter()
Http404
So we can implement the view as:
def bookings(request):
booking_list = get_list_or_404(Booking.objects.filter().order_by("-day"))
return render(request, 'roombooker/base.html', 'booking_list': booking_list)
Note: if you do not want to .filter() the queryset on anything, it is advisable to use .all() instead.
.filter()
.all()
In case you do not want to materialize the queryset into a list, we can implement the check ourselves with:
from django.http import Http404
def bookings(request):
booking_list = Booking.objects.all().order_by("-day")
if not book_list:
raise Http404('No bookings found')
return render(request, 'roombooker/base.html', 'booking_list': booking_list)
This solved the problem exactly, I need to do more research before posting. Thank you
– mogoli
Sep 3 at 20:34
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Use
get_list_or_404instead ofget_object_or_404, the latter will call.get()and that triggers the error.– Willem Van Onsem
Sep 3 at 19:58