Home > database >  type object 'Post' has no attribute 'filter'
type object 'Post' has no attribute 'filter'

Time:11-12

Every time I attempt to write a comment on a post, I get an AttirbuteError at the post number. e.g- 'AttributeError at /post/54/', and below this it says 'type object 'Post' has no attribute 'filter''. It then directs me to my views.py line 58, which reads: post = self.get_object(Post). It is a part of my PostDetailClass:

class PostDetailView(DetailView):
    model = Post
    
    form = CommentForm
    
    def post(self, request, *args, **kwargs):
        form = CommentForm(request.POST)
        if form.is_valid():
            post = self.get_object(Post)
            form.instance. user = request.user
            form.instance.post = post
            reply_id = request.POST.get('comment_id')
            comment_qs = None
            if reply_id:
                comment_qs = Comment.objects.get(id=reply_id)
            reply = comment_qs, reply=None
            form.save()
            form.save_m2m()

            return redirect(reverse("post", kwargs={
                'content': Post.content
            }))


urls.py

    from django.urls import path
    from django.conf import settings
    from django.conf.urls.static import static
    from django.urls.conf import re_path
    from django.views.generic.base import RedirectView
    from .views import ( 
        PostListView, 
        PostDetailView, 
        PostCreateView,
        PostDeleteView,
        UserPostListView,
        TagIndexView,
        about,
    )
    from . import views

    urlpatterns = [
        path('', PostListView.as_view(), name='blog-home'),
        path('post/<int:pk>/', PostDetailView.as_view(), name='post- 
   detail'),
        path('user/<str:username>', UserPostListView.as_view(), 
    name='user-posts'),
        path('post/new', PostCreateView.as_view(), name='post-create'),
        path('about/', views.about, name='blog-about'),
        path('map/', views.map, name='blog-map'),
        path('post/<int:pk>/delete/', PostDeleteView.as_view(), 
    name='post-delete'),
        path('latest-posts/', views.latest_posts, name='latest-posts'),
        path('focused/', views.focused, name='focused'),
        path('snakegame/',views.snake_game, name='snake-game'),
        re_path(r'^tag/(?P<slug>[-\w]*)/$',TagIndexView.as_view(), 
    name='tagged')

    ]  static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Can anyone point out what is wrong with my code? Thank you.

CodePudding user response:

The issue comes from passing Post to self.get_object(). get_object accepts a queryset as its argument. A queryset object would have .filter() but not Post.

In this case you actually don't need to pass anything to self.get_object. When you don't pass anything to it, the queryset defaults to self.get_queryset().

In short, change that line to:

post = self.get_object()

CodePudding user response:

you should try to get the post object first like below :-

object_id = self.kwargs[self.pk_url_kwarg]
post = self.model.objects.get(id=object_id)

in your post method :-

def post(self, request, *args, **kwargs):
    form = CommentForm(request.POST)
    if form.is_valid():
        object_id = self.kwargs[self.pk_url_kwarg]
        post = self.model.objects.get(id=object_id)
        form.instance.user = request.user
        form.instance.post = post
        reply_id = request.POST.get('comment_id')
        comment_qs = None
        if reply_id:
            comment_qs = Comment.objects.get(id=reply_id)
        reply = comment_qs, reply=None
        form.save()
        form.save_m2m()

        return redirect(reverse("post", kwargs={
            'content': post.content
        }))
  • Related