Django: use archive_index with date_field from a related model
-
12-09-2019 - |
Question
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 :)
Solution
From digging through the Django source code, the generic view archive_index
does not appear to support related fields that are GenericRelation
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow