2013-02-06 14 views
1

我有一個dayarchiveview在我的Django views.py文件我想繼承的dayarchiveview這樣我就可以使數據不同

class day_archive(DayArchiveView): 
    model=Timer 
    paginate_by=12 
    allow_future =True 
    allow_empty=True 
    month_format = '%m' 
    day_format='%d' 
    date_field='start_hour' 
    template_name='timer/timer_archive_date' 

    def get_queryset(self): 
     return Timer.objects.filter(author=self.request.user) 

,但我想使返回的數據爲表使用djangotables2的東西像這樣:

import django_tables2 as tables 

class Job_table(tables.Table): 
    class Meta: 
    model = Timer 
    attrs = {"class": "paleblue"} 
    fields= ('start_hour','end_hour','category','subcategory','duration') 

    def render_duration(self,value): 
     from timehuman import sectohour 
     hh= sectohour(value) 
     return hh 

我該如何渲染我的數據而不是呈現列表? (由Django的上下文object_list)我如何訪問將發送到object_list上下文並修改它的數據?

回答

0

這就是我最終所做的: 可能是hackish,但我讓我訪問到視圖的相同數據,我可以將它呈現爲表格。

class day_archive(DayArchiveView): 
    model=Timer 
    paginate_by=12 
    allow_future =True 
    allow_empty=True 
    month_format = '%m' 
    day_format='%d' 
    date_field='begin_time' 
    template_name='timer/timer_archive_date' 

    def get_queryset(self): 
     return Timer.objects.filter(author=self.request.user) 

    def render_to_response(self, context, **response_kwargs): 
     """ 
     Returns a response, using the `response_class` for this 
     view, with a template rendered with the given context. 

     If any keyword arguments are provided, they will be 
     passed to the constructor of the response class. 
     """ 

    tbl = context['object_list'] #this line is my hack, i dont know better. 
    if (tb1 != None): 
     jt = Timer_table(blo) 
     RequestConfig(self.request).configure(jt) 
     from django.db.models import Sum 
     total = tbl.aggregate(Sum('duration')) 
     t2 = total['duration__sum'] 
     if (t2 != None): 
      timedel= str(datetime.timedelta(seconds=float(t2))) 
      context['table']= jt 
      context['total'] = timedel 

    return self.response_class(
     request = self.request, 
     template = self.get_template_names(), 
     context = context, 
     **response_kwargs 
    ) 
相關問題