Python 在django管理中,如何将日期层次限制为年?

Python 在django管理中,如何将日期层次限制为年?,python,django,Python,Django,我需要在django管理中显示表中的一些数据。一切都安排好了,但我想知道我能做些什么,把date_层级限制为年。不允许在月份和日期内详细显示 以下是观点: 类HistorySummaryAdmin.ModelAdmin: change\u list\u template='admin/history\u summary\u change\u list.html' 日期层次结构='日期和时间' 列表\u筛选器= “设备类型”, def changelist_viewself、请求、额外_上下文=无

我需要在django管理中显示表中的一些数据。一切都安排好了,但我想知道我能做些什么,把date_层级限制为年。不允许在月份和日期内详细显示

以下是观点:

类HistorySummaryAdmin.ModelAdmin: change\u list\u template='admin/history\u summary\u change\u list.html' 日期层次结构='日期和时间' 列表\u筛选器= “设备类型”, def changelist_viewself、请求、额外_上下文=无: 响应=超级历史摘要管理员,self.changelist\u视图 要求 额外上下文=额外上下文, 尝试: qs=response.context_data['cl'].queryset 除AttributeError、KeyError外: 返回响应 指标={ “视图”:Countdate和time, } 选择部分={ “周”:CONCATYEARDATE\u ADDdate\u和时间,间隔-工作日日期和时间日,'-',LPADWEEKDATE\u ADDdate\u和时间,间隔-工作日日期和时间日,2,0, } response.context_data['summary']=list qs .extraselect=selectpart .价值观“一周” .注释**指标 .按“周”订购 summary_over_time=qs.extraselect=selectpart.values'week'。注释**度量。按'-View'排序 high=一段时间内的总结。首先['views'] low=一段时间内的摘要。最后一次['views'] summary_over_time=qs.extraselect=selectpart.values'week'。注释**度量。按'week'排序 response.context_data['summary_over_time']=[{ “周”:x[“周”], “视图”:x[“视图”], “pct”:x[“视图”]*100/高, }对于汇总中的x,随时间变化] 返回响应 和我的模板:

<div class="results">
    <h2> Views over time </h2>
    <div class="bar-chart-graph">
        <div class="bar-chart">
        {% for x in summary_over_time %}
            <div class="bar" style="height:{{x.pct}}%">
                <div class="bar-tooltip">
                    <p>{{x.week}}</p>
                    <p>{{x.views | default:0 }} views</p>
                </div>
            </div>
        {% endfor %}
        </div>
    </div>
    <table>
        <thead>
          <tr>
            <th>
              <div class="text">
                <a href="#">Week</a>
              </div>
            </th>
            <th>
              <div class="text">
                <a href="#">Views</a>
              </div>
            </th>
          </tr>
        </thead>
        <tbody>
          {% for row in summary %}
          <tr class="{% cycle 'row1' 'row2' %}">
            <td> {{ row.week }} </td>
            <td> {{ row.views | intcomma }} </td>
          </tr>
          {% endfor %}
        </tbody>
    </table>
</div>

我想让用户选择一年,而不是一个月

是的:

否:

好的,我自己解决了这个问题。以下是方法:

我从SimpleListField类创建了一个名为DateYearFilter的过滤器。代码如下:

class DateYearFilter(admin.SimpleListFilter):
    title = 'year'
    parameter_name = 'date_and_time'

    def lookups(self, request, model_admin):
        #Choices to propose are all the years from the start
        firstyear = History.objects.order_by('date_and_time').first().date_and_time.year #First year of the history
        currentyear = datetime.datetime.now().year  #Current year
        years = [] #Declaration of the list that'll contain the missing years
        for x in range(currentyear-firstyear):  #Fill the list with the missing years
            yearinloop = firstyear+x
            years.insert(0,(str(yearinloop),str(yearinloop)))
        years.insert(0,(str(currentyear),str(currentyear)))
        return years

    def queryset(self, request, queryset):
        if self.value(): #If a year is set, we filter by year else not
            return queryset.filter(date_and_time__year=self.value())
        else:
            return queryset
然后我从admin类中删除了date层次结构,并添加了这个过滤器,所以它有点类似。 代码如下:

class DateYearFilter(admin.SimpleListFilter):
    title = 'year'
    parameter_name = 'date_and_time'

    def lookups(self, request, model_admin):
        #Choices to propose are all the years from the start
        firstyear = History.objects.order_by('date_and_time').first().date_and_time.year #First year of the history
        currentyear = datetime.datetime.now().year  #Current year
        years = [] #Declaration of the list that'll contain the missing years
        for x in range(currentyear-firstyear):  #Fill the list with the missing years
            yearinloop = firstyear+x
            years.insert(0,(str(yearinloop),str(yearinloop)))
        years.insert(0,(str(currentyear),str(currentyear)))
        return years

    def queryset(self, request, queryset):
        if self.value(): #If a year is set, we filter by year else not
            return queryset.filter(date_and_time__year=self.value())
        else:
            return queryset
类HistorySummaryAdmin.ModelAdmin: change\u list\u template='admin/history\u summary\u change\u list.html' 按设备类型和年份过滤,因此数据按年份重新分组 列表_过滤器=['device_type',DateYearFilter] 现在显示的不是带有日期的条形图,而是:

我希望它能帮助需要这样做的人: