Django Using order_by with .annotate() and getting related field

╄→尐↘猪︶ㄣ 提交于 2019-12-24 03:07:09

问题


I have the following data,

This query groups by topicid, and then in each group gets the max date, frequency of posts and counts the number of authors as contributors,

    info_model = InfoModel.objects.values('topicid')
            .annotate( max=Max('date'), freq=Count('postid'),                   
             contributors=Count('author', distinct=True))

This query can then be displayed as follows,

Q.1 (SOLVED) How can I order the rows by date, from most recent down? I did appended .order_by('date') to the query, which seems like the most obvious solution but this produces,

completely changing the 'freq' and 'contributions'.

EDIT: The ordering can be achieved by appending .order_by('-max')

Q.2 How can I display the 'post' for that date? So the post column should display,

ya

see ya

ciao

yoyo

I thought the following should work with {{ item.post }}, but no such luck.

  <table class='table table-striped table-hover'>
        <thead>
          <tr>
            <th>freq</th>
            <th>topicid</th>
            <th>date</th>
            <th>contributors</th>
            <th>post</th>
          </tr>
        </thead>
        <tbody>
          {% for item in info %}
          <tr>
            <td>{{ item.freq }}</td>
            <td>{{ item.topicid }}</td>
            <td>{{ item.max }}</td>
            <td>{{ item.contributors }}</td>
            <td>{{ item.post }}</td>
          </tr>
          {% endfor %}
        </tbody>
      </table>

Thanks,

Edit:

I can get the correct result with raw SQL but can't do it wth a Django query,

info_model = list(InfoModel.objects.raw('SELECT *, 
              max(date),  
              count(postid) AS freq,     
              count(DISTINCT author) AS contributors FROM        
              crudapp_infomodel GROUP BY topicid ORDER BY date DESC'))

I simplified and reposted this problem here Rewrite raw SQL as Django query


回答1:


The following view amalgamates two queries to solve the problem,

def info(request):
    info_model = InfoModel.objects.values('topic')
                 .annotate( max=Max('date'), 
                 freq=Count('postid'), 
                 contributors=Count('author', distinct=True))
                 .order_by('-max')

    info2 = InfoModel.objects.all()

    columnlist = []
    for item in info2:
         columnlist.append([item])

    for item in info_model:
        for i in range(len(columnlist)):
            if item['max'] == columnlist[i][0].date:
                item['author'] = columnlist[i][0].author
                item['post'] = columnlist[i][0].post
                print item['max']

    paginator = Paginator(info_model, 20)
    page = request.GET.get('page')
    try:
        info = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        info = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        info = paginator.page(paginator.num_pages)
    return render(request, 'info.html', {'info': info})


来源:https://stackoverflow.com/questions/37891219/django-using-order-by-with-annotate-and-getting-related-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!