python - Django:使用相关模型中的 archive_index 和 date_field

标签 python django django-models django-views django-generic-views

你好(请原谅我的英语不好:p),

想象一下这两个简单的模型:

from django.contrib.contenttypes import generic
from django.db import models

class SomeModel(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField(_('object id'))
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    published_at = models.DateTimeField('Publication date')


class SomeOtherModel(models.Model):
    related = generic.GenericRelation(SomeModel)

我想将 archive_index 通用 View 与 SomeOtherModel 一起使用,但它不起作用:

from django.views.generic.date_based import archive_index

archive_index(request, SometherModel.objects.all(), 'related__published_at')

错误来自 archive_index 第 28 行(使用 django 1.1):

date_list = queryset.dates(date_field, 'year')[::-1]

引发的异常是:

SomeOtherModel has no field named 'related__published_at'

你有解决办法吗?

非常感谢:)

最佳答案

通过深入研究 Django 源代码,通用 View archive_index 似乎不支持 GenericRelation 的相关字段。

这是因为查询集方法dates不支持泛型关系。考虑将此作为错误/功能请求提交到 Django 错误跟踪器上。

关于python - Django:使用相关模型中的 archive_index 和 date_field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1453465/

相关文章:

python - 如何为现有项目生成 asgi.py?

django - 如何在 django 中从 QueryDict 创建对象?

python - Django Model API 通过中间表反向查找多对多关系

python - 如何创建包含 threading.Event 和 multiprocessing.Event 的协议(protocol)?

python - 创建一个数字序列并在达到特定数字时重置自身

python - 如何在 Python 中将\xXY 编码的字符转换为 UTF-8?

python - 如何在 python 中更快地进行搜索?

html - 如何将内容从 Django 的页面传递到侧边栏?

Python3.4 + Django + Ubuntu : unknown encoding: raw-unicode-escape

python - Django 每月/每季度对 DateField() 数据进行分组