0

我正在使用 Appengine 1.6.5,从我得到 None 的 jinja2 模板访问 NDB 模型类属性,而是在循环上访问相同的属性得到了正确的结果。为了解释问题,我发布了一些代码

- - 楷模 - -

class Vehicles(ndb.Model):
    registration = ndb.StringProperty()
    brand = ndb.StringProperty()
    model = ndb.StringProperty()
    cost = ndb.FloatProperty()
    km_cost = ndb.FloatProperty()
    viacard = ndb.StringProperty()
    telepass = ndb.StringProperty()

class Employees(ndb.Model):
    user_id = ndb.StringProperty()
    name = ndb.StringProperty()
    surname = ndb.StringProperty()
    email = ndb.StringProperty()
    job = ndb.StringProperty()
    credit_card = ndb.StringProperty()
    cc_expiry_date = ndb.StringProperty()
    vehicle = ndb.KeyProperty(kind=Vehicles)
    last_login = ndb.DateTimeProperty(auto_now=True)
    last_activity = ndb.DateTimeProperty(auto_now=True)

    @property
    def vehicle_registration(self):
        return Key(self.vehicle.kind(),int(self.vehicle.id())).get().registration

--- 处理程序 --- 这只是处理程序的一部分

class BaseHandler(webapp2.RequestHandler):

    @webapp2.cached_property
    def jinja2(self):
        return jinja2.get_jinja2(app=self.app)

    def render_template(self, filename, **kwargs):
        self.response.write(self.jinja2.render_template(filename, **kwargs))

class EmployeesHandler(BaseHandler):
    @login_required
    def get(self,**kwargs):
        user = users.get_current_user()
        company = models.Company.query().fetch(1)
        employees = models.Employees.query().fetch()
        vehicles = models.Vehicles.query().fetch()

        params = {
            "nickname" : user.nickname(),
            "company" : company,
            "employees" : employees,
            "vehicles" : vehicles
            }
        return self.render_template('employee.html', **params)

--- 模板 --- 这是模板的一部分

<table class="table table-striped table-bordered table-condensed">
  <thead>
    <tr>
      <th>Nome</th>
      <th>Cognome</th>
      <th>Email</th>
      <th>Ruolo</th>
      <th>Carta di credito</th>
      <th>Scadenza</th>
      <th>Auto</th>
      <th>Ultimo accesso</th>
      <th>Ultimo movimento</th>
    </tr>
  </thead>
  <tbody>
    {% for emp in employees %}
    <tr>
      <td>{{ emp.name }}</td>
      <td>{{ emp.surname }}</td>
      <td>{{ emp.email }}</td>
      <td>{{ emp.job }}</td>
      <td>{{ emp.credit_card }}</td>
      <td>{{ emp.cc_expiry_date }}</td>
      <td>({{ emp.vehicle.vehicle_registration }})</td>
      <td>{{ emp.last_login }}</td>
      <td>{{ emp.last_activity }}</td>
    </tr>
    {% endfor %}
  </tbody>
</table>

谁能告诉我获取雇员汽车登记号的正确方法?提前致谢

问候,罗伯托

4

1 回答 1

0

尝试:emp.vehicle.get.vehicle_registration

但是,您遇到了n+1 问题,您应该真正考虑预取车辆参考。

于 2012-06-14T16:29:22.010 回答