python - 字段错误: Cannot resolve 'publish_year' into field

标签 python django

我是 Django 新手,我目前正在使用 django,例如,我收到此错误 FieldError:无法将 'publish_year' 解析为字段

这是我的模型:

# Abstract Model

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='publish')
    author = models.ForeignKey(User, 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')


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

    objects = models.Manager()
    published = PublishedManager()

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

    def get_absolute_url(self):
        return reverse('post_detail',
                            args=[self.publish.year,
                                self.publish.strftime('%m'),
                                self.publish.strftime('%d'),
                                self.slug])

     def __str__(self):
        return self.title

这是我尝试编辑的观点,但我认为问题不在于我的观点

def post_list(request):
    posts = Post.objects.all()
    return render(request, '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)
    return render(request, 'detail.html',
                            {'post':post})
    enter code here
    enter code here

当我尝试访问帖子的详细信息时,出现以下错误消息

FieldError at /2017/03/15/help/
Cannot resolve keyword 'publish_year' into field. Choices are: author, author_id, body, created, id, publish, slug, status, title, updated
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/2017/03/15/help/
    Django Version: 1.10.6
    Exception Type: FieldError
    Exception Value:    
    Cannot resolve keyword 'publish_year' into field. Choices are: author, author_id, body, created, id, publish, slug, status, title, updated
    Exception Location: C:\Users\Harsley\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\query.py in names_to_path, line 1327
    Python Executable:  C:\Users\Harsley\AppData\Local\Programs\Python\Python36-32\python.exe
    Python Version: 3.6.0
    Python Path:    
    ['C:\\Users\\Harsley\\blog',
     'C:\\Users\\Harsley\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
     'C:\\Users\\Harsley\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
     'C:\\Users\\Harsley\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
     'C:\\Users\\Harsley\\AppData\\Local\\Programs\\Python\\Python36-32',
     'C:\\Users\\Harsley\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages']
    Server time:    Wed, 15 Mar 2017 15:31:16 +0000

最佳答案

您应该使用下划线for lookups that span relations ,例如发布__year:

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

关于python - 字段错误: Cannot resolve 'publish_year' into field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42815709/

相关文章:

python - 3 Quicksorts 函数 - 为什么 lambda 版本更慢?提供代码

Python/Bokeh - FuncTickFormatter

django - 在反向关系上访问 prefetch_related 的字段

python - Django 会自动向我的应用程序添加测试吗?

python - 我的 models.py 有什么问题

python - 使用 Windows 命令提示符或 PowerShell 在 Spyder 中运行脚本

python - Pandas :如何过滤数据帧中至少出现 n 次的重复项目的数据帧

python - 如何在python中的特定字符后拆分字符串

python - Numpy 作为 django 项目中的库

django - 具有 CORS 文件上传功能的 Amazon S3; s3upload.js 示例