【问题标题】:Facing issue in custom manager of djangoDjango 自定义管理器面临的问题
【发布时间】:2021-10-14 21:59:16
【问题描述】:

我正在尝试创建一个自定义管理器来检索具有 已发布 状态的所有帖子。经理新手!!提前谢谢你

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})

错误

“PublishedManager”对象没有属性“all”(views.py,第 6 行,在 post_list 中)

【问题讨论】:

    标签: python django django-models django-views django-custom-manager


    【解决方案1】:

    您应该使用Manager 作为经理的基类,而不是Model。并且方法名应该是get_queryset 而不是get_query_set

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

    您可以在docs找到更多详细信息。

    【讨论】:

      【解决方案2】:

      您的PublishManager 应该是Manager,而不是Model。此外,覆盖的方法是get_queryset,而不是get_query_set

      #                  use Manager ↓
      class PublishedManager(models.Manager):
      
          # not get_query_set ↓
          def get_queryset(self):
              return super().get_queryset().filter(status='published')

      在视图中,您可能希望使用published 管理器来防止重复相同的逻辑,因此:

      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})

      【讨论】:

        猜你喜欢
        • 2011-04-20
        • 1970-01-01
        • 2018-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-17
        相关资源
        最近更新 更多