【问题标题】:AttributeError - 'DeferredAttribute' object has no attribute 'all'AttributeError - 'DeferredAttribute' 对象没有属性 'all'
【发布时间】:2019-01-20 06:13:33
【问题描述】:

posts = Post.published.all() 中不断出现错误

models.py

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse


class Post(models.Model):
STATUS_CHOICES = (
     ('draft','Draft'),
     ('published','Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250,
                         unique_for_date='published')
author = models.ForeignKey(User,
                           related_name='blog_posts',
                           on_delete=models.CASCADE)
body = models.TextField()
published = 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')
def get_absolute_url(self):
    return reverse('blog:post_detail',
                   args=[self.publish.year,
                         self.publish.strftime('%m'),
                         self.publish.strftime('%d'),
                         self.slug])

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


def __str__(self):
    return self.title  

views.py

from django.shortcuts import render, get_object_or_404
from .models import Post

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

def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post,
                               status='published',
                               publish_year=year,
                               publish_month=month,
                               publish_day=day)

每当我运行我的服务器时,我都会收到此错误...

AttributeError at /blog/ 'DeferredAttribute' object has no attribute 'all'

from django.shortcuts import render, get_object_or_404
from .models import Post
def post_list(request):
posts = Post.published.all() ...

还有一些制作我自己的博客页面的提示会有所帮助

【问题讨论】:

    标签: django python-3.x django-models django-views attributeerror


    【解决方案1】:

    您需要使用objects 而不是published

    posts = Post.objects.all()
    

    如果您想过滤published 帖子,请使用:

    posts = Post.objects.filter(status='published')
    

    【讨论】:

      【解决方案2】:

      要让它与自定义发布管理器一起使用,请将其添加到模型文件中的帖子类顶部。

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

      然后在 Post 类中添加管理器

      已发布 = PublishedManager()

      【讨论】:

        【解决方案3】:

        获取所有帖子:

        posts = Post.objects.all() 
        

        要获取已发布的帖子:

        posts = Post.objects.filter(status='published')
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-12
          • 2021-07-06
          • 2017-06-16
          • 1970-01-01
          • 1970-01-01
          • 2012-12-01
          • 2021-04-19
          • 2021-11-22
          相关资源
          最近更新 更多