[Answered ]-MultipleObjectsReturned: get() returned more than one Post β€” it returned 2

1πŸ‘

βœ…

It means that there are two Posts with as slug mypage. Note that you do not filter on the author of your Post, so if two author’s have a mypage, then this will raise an error.

You can filter on the author slug with:

class PostUpdateView(LoginRequiredMixin, UpdateView):
    model = Post 
    form_class = PostForm

    def get_queryset(self, *args, **kwargs):
        return super().get_queryset(*args, **kwargs).filter(
            author__username=self.kwargs['author']
        )
    
    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

Leave a comment