[Django]-Django MultipleObjectsReturned

12👍

If you want to return all entries with author=user, then use filter()

entries = Entries.objects.filter(author=user)

At the moment, you are using get(), which expects to return one Entries object. As there is more that one Entries with author=user, you get the Entries.MultipleObjectsReturned error.

Note, with Django, the convention is to use the singular name Entry for your model, instead of the plural Entries.

4👍

You’re using get, when you should be using filter.

Only use get when there’s only one possible result. In that case, you get back the object itself, not a queryset. There is then nothing to loop over, so the for is extraneous.

If you actually expect a queryset, you must use filter.

Leave a comment