[Django]-Type object 'Post' has no attribute 'published' Django

8👍

If I correctly understand then you have a typographical mistake

similar_posts = Post.published.filter(tags__in=post_tags_ids)
                                     .exclude(id=instance.id)

The line above should be

similar_posts = Post.objects.filter(tags__in=post_tags_ids)
                                     .exclude(id=instance.id)

Also If you meant use field publish then it can only be used in queryset args not as a Related Object attribute

0👍

Add the following attributes to the Post class:

class PublishedManager(models.Manager):
      def get_queryset(self):
            return super(PublishedManager,self).get_queryset().filter(status='published')

class Post(models.Model):
   # ...
   objects = models.Manager() # The default manager.
   published = PublishedManager() # Our custom manager.

and

posts = Post.published.all()                          

Leave a comment