无法计算具有多对多关系的app引擎模型

时间:2013-12-23 03:09:15

标签: python html google-app-engine jinja2

我想获得单个用户的事件计数。

我的模特:

class User(db.Model):
    name = db.StringProperty(required = True)
    pw_hash = db.StringProperty(required = True)
    email = db.StringProperty(required = True)

class PlaceModel(db.Model):
    place_name = db.StringProperty(required = True)
    place_lat = db.StringProperty(required = True)
    place_long = db.StringProperty(required = True)
    def render(self):
        return render_str("placemodel.html", p = self)

class EventModel(db.Model):
    event_place = db.ReferenceProperty(PlaceModel)
    event_people = db.ReferenceProperty(User)

    event_name = db.StringProperty(required = True)
    event_time = db.StringProperty(required = True)
    def render(self):
        return render_str("eventmodel.html", e = self)

蟒:

class MainPage(Handler): #render all userevents
    def get(self):
        email = self.session.get('foo')
        user = User.all().filter("email", email).get()
        userevents = EventModel.all().filter("event_people =", user).get()
        self.render("mainpage.html", userevents = userevents)

HTML:

{%if userevents.count() == 0%}
    <div>no events for now</div>
{% else %}
    {% for event in userevents %}
        {{event.event_name}}
        {{event.event_time}}
    {% endfor %}
{% endif %}

当我运行此操作时,我收到一条错误消息,指出EventModel对象没有属性计数。如何计算与用户匹配的EventModel实例数?

2 个答案:

答案 0 :(得分:1)

.get()查询的末尾使用EventModel.all(),您已通过获取第一个值来解析查询。这意味着userevents不是具有count()方法的查询,而是实际的单个EventModel实例。删除.get()

答案 1 :(得分:0)

只需使用userevents.count即可获取用户事件的计数。