Django - Mongoengine Group By + Where + Sort +限制和打印结果

问题描述:

我向我的数据库查询并选择收集评论。我想过滤,整理,分组,排序和限制查询结果。Django - Mongoengine Group By + Where + Sort +限制和打印结果

查询在views.py。它工作正常,但我有一个问题,在html文件中打印上下文变量“best_hotels”的内容。

from __future__ import unicode_literals 
from django.db import models 
from mongoengine import * 

class Reviews(Document): 
    # _id = IntField(primary_key=True) 
    content_lenght = IntField() 
    title_score = IntField() 
    content_eval = StringField() 
    review_stars = FloatField() 
    hotel_name = StringField() 
    review_score = IntField() 
    city = StringField() 
    helpful_reader = IntField() 
    title = StringField() 
    content_score = IntField() 
    stars_eval = StringField() 
    content = StringField() 
    title_eval = StringField() 
    review_eval = StringField() 

这里是views.py

from el_pagination.decorators import page_templates 
from django.shortcuts import render 
from django.http import HttpResponse 
from models import Reviews, Evaluation 

def reviews(request): 

    best_hotels = Reviews.objects.aggregate(
     { 
      "$match": {"review_eval": "positiv"} 
     }, 
     { 
      "$group" : {"_id" : "$hotel_name", "sum" : { "$sum" : 1 } } 
     }, 
     { 
      "$sort" : {"sum" : -1} 
     }, 
     { 
      "$limit": 10 
     } 
    ) 
    context = { 
    'best_hotels' : best_hotels 
    } 
    return render(request, 'review/reviews.html', context) 

她是reviews.html

... 
    <h2>The 10 best hotels with the most positiv ratings</h2> 
    <ul> 
    {% for row in best_hotels %} 
     <li>Name: {{ row.id }} - {{ row.sum }} positiv ratings</li> 
    {% endfor %} 
    </ul> 
... 

这里HTML

The 10 best hotels with the most positiv ratings 

Name: - 678 positiv Ratings 
Name: - 387 positiv Ratings 
Name: - 364 positiv Ratings 
Name: - 305 positiv Ratings 
Name: - 292 positiv Ratings 
Name: - 269 positiv Ratings 
Name: - 267 positiv Ratings 
Name: - 224 positiv Ratings 
Name: - 219 positiv Ratings 
Name: - 181 positiv Ratings 

酒店的名称的结果不见了。为什么?

我更改了html文件来检查酒店的名称是否存在。这里是新的:

<h2>The 10 best hotels with the most positiv ratings</h2> 
<ul> 
{% for row in best_hotels %} 
    <li>{{ row}}</li> 
{% endfor %} 
</ul> 

这里是新的HTML文件

{u'sum': 678, u'_id': u' Steigenberger Airport Hotel '} 
{u'sum': 387, u'_id': u' Steigenberger Frankfurter Hof '} 
{u'sum': 364, u'_id': u' Sheraton Frankfurt Airport Hotel & Conference Center '} 
{u'sum': 305, u'_id': u' Wyndham Grand Frankfurt '} 
{u'sum': 292, u'_id': u' Innside by Meli\xe1 Frankfurt Niederrad '} 
{u'sum': 269, u'_id': u' Innside by Meli\xe1 Frankfurt Eurotheum '} 
{u'sum': 267, u'_id': u' Hilton Garden Inn Frankfurt Airport '} 
{u'sum': 224, u'_id': u" 25hours Hotel by Levi's "} 
{u'sum': 219, u'_id': u' 25hours Hotel The Goldman '} 
{u'sum': 181, u'_id': u' The Westin Grand Frankfurt '} 

在这里你可以看到,hotelnames在那里的结果。但为什么不在第一个HTML文件?

我不明白你在第一个html文件中打印名字的地方。在li标签中没有元素作为{{row.name}}。

+0

酒店的名称在{{row.id}}变量 –

+0

其可能是因为_id然后,而不是id。 – Aayushi

+0

变量和属性不能以下划线开头。 我无法使用{{row._id}}或{{_id}} –