如何在googleappengine中从实体列表向下钻取到实体实例?

2024-10-04 15:19:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个实体列表,希望使用实体键链接到单个实体的更多详细信息

class RouteDetails(ndb.Model):
    """Get list of routes from Datastore """
    RouteName = ndb.StringProperty()

    @classmethod
    def query_routes(cls):
        return cls.query().order(-cls.RouteName)


class RoutesPage(webapp2.RequestHandler):
    def get(self):
        adminLink = authenticate.get_adminlink()
        authMessage = authenticate.get_authmessage()
        self.output_routes(authMessage,adminLink)

    def output_routes(self,authMessage,adminLink):
        self.response.headers['Content-Type'] = 'text/html'
        html = templates.base
        html = html.replace('#title#', templates.routes_title)
        html = html.replace('#authmessage#', authMessage)
        html = html.replace('#adminlink#', adminLink)
        html = html.replace('#content#', '')
        self.response.out.write(html + '<ul>')
        list_name = self.request.get('list_name')
        #version_key = ndb.Key("List of routes", list_name or "*notitle*")
        routes = RouteDetails.query_routes().fetch(20)

        for route in routes:
            routeLink = '<a href="route_instance?key={}">{}</a>'.format(
                route.Key, route.RouteName)
            self.response.out.write('<li>' + routeLink + '</li>')
        self.response.out.write('</ul>' + templates.footer)

我得到的错误是AttributeError: 'RouteDetails' object has no attribute 'Key'

如何在我的drilldownURL中引用实体的唯一ID


Tags: self实体getresponsedefhtmlroutereplace
1条回答
网友
1楼 · 发布于 2024-10-04 15:19:44

RouteDetails对象确实没有Key属性,因此您将在route.Key处得到一个异常

要获取实体的密钥,需要调用key属性/属性:route.key

但是通过HTML直接传递实体的键是不起作用的,因为它是一个对象。urlsafe()方法可用于提供可以在HTML中使用的键对象的字符串表示

因此,请按照以下思路来做:

    for route in routes:
        routeLink = '<a href="route_instance?key={}">{}</a>'.format(
            route.key.urlsafe(), route.RouteName)
        self.response.out.write('<li>' + routeLink + '</li>')

另见Linking to entity from list

相关问题 更多 >

    热门问题