Facing issue in custom manager of django

65 Views Asked by At

I am trying to create a custom manager to retrieve all posts with the published status. New to managers!! Thank you in advance <3.

models.py


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


class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published'),
    )
    title = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255, unique_for_date='publish')
    author = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='blog_posts')
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(
        max_length=10, choices=STATUS_CHOICES, default='draft')
    objects = models.Manager()
    published = PublishedManager()

    class Meta:
        ordering = ('-publish',)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog:post_detail', args=[self.publish.year, self.publish.month, self.publish.day, self.slug])

views.py

def post_list(request):
    posts = Post.published.all()
    print(posts)
    return render(request, 'post/list.html', {'posts': posts})


def post_detail(request):
    post = get_object_or_404(Post, slug=post, status='published',
                             publish__year=year, publish__month=month, publish__day=day)

    return render(request, 'post/detail.html', {'post': post})

Error

'PublishedManager' object has no attribute 'all' (views.py, line 6, in post_list)

3

There are 3 best solutions below

0
On BEST ANSWER

You should use Manager as a base class for manager, not Model. And method name should be get_queryset instead of get_query_set:

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

You can find more details in the docs.

0
On

Your PublishManager should be a Manager, not a Model. Furthermore the method to override is get_queryset, not get_query_set:

#                  use Manager ↓
class PublishedManager(models.Manager):

    # not get_query_set ↓
    def get_queryset(self):
        return super().get_queryset().filter(status='published')

In the view, you might want to use the published manager to prevent repeating the same logic, so:

def post_detail(request):
    post = get_object_or_404(
        Post.published.all(),
        slug=post, status='published', publish__year=year,
        publish__month=month, publish__day=day
    )

    return render(request, 'post/detail.html', {'post': post})
0
On

From the book, Django 4 By Example by Antonio Mele:

# Model Manager
class PublishedManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(status = Post.Status.PUBLISHED)

# Models
class Post(models.Model):
    class Status(models.TextChoices):
        DRAFT = 'DF', 'Draft'
        PUBLISHED = 'PB', 'Published'
    title = models.CharField(max_length = 250)
    slug = models.SlugField(max_length = 250)
    author = models.ForeignKey(User, on_delete = models.CASCADE, related_name = 'blog_posts')
    body = models.TextField()
    publish = models.DateTimeField(default = timezone.now)
    created = models.DateTimeField(auto_now_add = True)
    updated = models.DateTimeField(auto_now = True)
    status = models.CharField(max_length = 2, choices = Status.choices, default = Status.DRAFT)
    objects = models.Manager() # Default Model Manager
    published = PublishedManager() # Custom Model Manager
    class Meta: