gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:39:16 25 4
gpt4 key购买 nike

你好(请原谅我的英语不好: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com