Django:使用相关模型中的 archive_index 和 date_field
你好(请原谅我丑陋的英语: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 通用视图与 SomeOtherModel 一起使用,但它不起作用:
from django.views.generic.date_based import archive_index
archive_index(request, SometherModel.objects.all(), 'related__published_at')
错误来自第 28 行的 archive_index (使用 django 1.1 ):
date_list = queryset.dates(date_field, 'year')[::-1]
提出的例外是:
SomeOtherModel has no field named 'related__published_at'
您有解决办法吗?
非常感谢 :)
Hello (please excuse me for my ugly english :p),
Imagine these two simple models :
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)
I would like to use the archive_index generic view with SomeOtherModel, but it doesn't work :
from django.views.generic.date_based import archive_index
archive_index(request, SometherModel.objects.all(), 'related__published_at')
The error comes from archive_index at line 28 (using django 1.1) :
date_list = queryset.dates(date_field, 'year')[::-1]
The raised exception is :
SomeOtherModel has no field named 'related__published_at'
Have you any idea to fix it ?
Thank you very much :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过深入研究 Django 源代码,通用视图
archive_index
似乎不支持GenericRelation
相关字段。这是因为查询集方法
dates
不支持泛型关系。考虑将此作为错误/功能请求提交到 Django 错误跟踪器上。From digging through the Django source code, the generic view
archive_index
does not appear to support related fields that areGenericRelation
s.This is because the queryset method
dates
does not support generic relations. Consider filing this as a bug / feature request on the Django bug tracker.