Opened 6 years ago

Last modified 6 months ago

#28048 new New feature

Allow generic date views to use related fields as date_field

Reported by: Lefteris Nikoltsios Owned by:
Component: Generic views Version: 1.11
Severity: Normal Keywords:
Cc: Jaap Roes Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

For example if we have some models like that:

class Blog(models.Model):
    title = models.TextField()
    pubdate = models.DateField()

class Article(models.Model):
    blog = models.ForeignKey(Blog)
    title = models.TextFIeld()
    pubdate = models.DateField()

the following view:

from django.views.generic.dates import MonthArchiveView

from .models import Article


class ArticleMonthArchiveView(MonthArchiveView):
    queryset = Article.objects.all()
    date_field = 'blog__pub_date'

will raise FieldDoesNotExist.

Change History (6)

comment:1 Changed 6 years ago by Lefteris Nikoltsios

Has patch: set

comment:2 Changed 6 years ago by Tim Graham

Patch needs improvement: set
Summary: Generic date views don't work with related fields as date_fieldAllow generic date views to use related fields as date_field
Triage Stage: UnreviewedAccepted
Type: UncategorizedNew feature

comment:3 Changed 6 years ago by Lefteris Nikoltsios

Patch needs improvement: unset

comment:4 Changed 6 years ago by Carlton Gibson

Patch needs improvement: set

comment:5 Changed 2 years ago by Jaap Roes

Cc: Jaap Roes added

I created issue #32846 which was marked as a duplicate. Looking at the patch provided for this ticket is seems that the most difficult part is the lack of an "official" way of resolving "field paths" like blog__pub_date to fields. Django admin provides some utility methods, but since it is a contrib app it cannot be used in other "core" Django code.

Is there a separate ticket that tracks this? (I couldn't find one.) The addition of a canonical way to resolve field paths could also be helpful for 3rd party apps, for example django-filter has it's own implementation: https://github.com/carltongibson/django-filter/blob/f507201addfb7f105e722573f3ced4b4a9870526/django_filters/utils.py#L143, and I'm certain there are others.

comment:6 Changed 6 months ago by Mariusz Felisiak

Owner: Lefteris Nikoltsios deleted
Status: assignednew
Note: See TracTickets for help on using tickets.
Back to Top