AttributeError: объект 'ManyToManyDescriptor' не имеет атрибута 'remove'
Я пытаюсь сделать систему симпатий с LikeView
и BlogDetails
, например, если мне понравилось, а потом я нажимаю еще раз, то мне не понравилось
def LikeView(request, pk):
post = get_object_or_404(Post, id=request.POST.get('post_id'))
if post.likes.filter(id=request.user.id).exists():
Post.likes.remove(id=request.user.id)
liked = False
else:
post.likes.add(request.user)
liked = True
return HttpResponseRedirect(reverse_lazy('blog-detail', args=[str(pk)]))
class BlogDetails(DetailView):
model = Post
template_name = 'blog_details.html'
def get_context_data(self, *args, **kwargs):
categories = Category.objects.all()
context = super(BlogDetails, self).get_context_data(*args, **kwargs)
context['categories'] = categories
liked = False
stuff = get_object_or_404(Post, id=self.kwargs['pk'])
total_likes = stuff.total_likes()
context['likes'] = total_likes
if stuff.likes.filter(id=self.request.user.id).exists():
Liked = True
context['liked'] = liked
return context
и выдает ошибку AttributeError: 'ManyToManyDescriptor' object has no attribute 'remove'
Сделайте это как :-
def LikeView(request, pk):
post = get_object_or_404(Post, id=request.POST.get('post_id'))
if post.likes.filter(id=request.user.id).exists():
post.likes.remove(id=request.user) # Changed Here
liked = False
else:
post.likes.add(request.user)
liked = True
return HttpResponseRedirect(reverse('blog-detail', args=[str(pk)]))