【问题标题】:Selecting objects from <select> form (HTML)从 <select> 表单中选择对象 (HTML)
【发布时间】:2011-06-22 01:27:47
【问题描述】:

我正在学习如何使用 Google App Engine / Python。 (网络应用)

我来自 Java(可能是我的问题!),如果我将对象放在 Swing 列表框中,它将调用它们的 toString() 方法以进行显示。当我选择其中一个时,它将返回对象,而不仅仅是由 toString() 生成的表示。

我有一个包含个人详细信息的 Person 模型:

class Person(db.Model):
  '''represents a single person'''
  first = db.StringProperty()
  last  = db.StringProperty()
  address = db.StringProperty()
  city  = db.StringProperty()
  region = db.StringProperty()
  postal = db.StringProperty()
  country = db.StringProperty()
  phone = db.StringProperty()
  cell = db.StringProperty()
  email = db.StringProperty()
  comment = db.StringProperty(multiline=True)

还有一个 Reservation 模型,用于存储有关房间的信息,并使用 Reservation 存储 Person 属于什么:

class Reservation(db.Model):
    '''represents a single reservation'''
    room = db.StringProperty()
    start_day = db.IntegerProperty()
    start_month = db.IntegerProperty()
    start_year  = db.IntegerProperty()
    end_day = db.IntegerProperty()
    end_month = db.IntegerProperty()
    end_year = db.IntegerProperty()
    percent_discount = db.IntegerProperty()
    comment = db.StringProperty(multiline=True)
    client = #what would go here?

为了从用户那里获取预订信息,并将预订放入数据库,我有一个 HTML 表单,其中包含房间、到达日期、离开日期等多个字段。其中一个包含一个“客户列表”,它可以获取已在数据库中的人员列表:

    <select size="5" name="client_list"> 
    {% for person in clients %}
        <option>{{ person.first|escape }} {{ person.last|escape }}</option>
    {% endfor %}
</select>

这可以显示名称,但是我不知道如何将选择的“人”或客户端存储到以下代码中的预订中:

class Bookings(webapp.RequestHandler):
    '''Handles all of the bookings'''
    def post(self):
        '''adds a new booking into the db'''
        reservation = models.Reservation()

        reservation.room = self.request.get('room')
        reservation.start_day = int(self.request.get('start_day'))
        reservation.start_month = util.month_to_int(self.request.get('start_month'))
        reservation.start_year = int(self.request.get('start_year'))
        reservation.end_day = int(self.request.get('end_day'))
        reservation.end_month = util.month_to_int(self.request.get('end_month'))
        reservation.end_year = int(self.request.get('end_year'))
        reservation.percent_discount = int(self.request.get('percent_discount'))
        reservation.comment = self.request.get('comment')
        #This would get the clients first + last name, but not the object Person
        #reservation.client = self.request.get('client_list')

        reservation.put()
        self.redirect('/bookings')

我开始研究 Keys(特别是 db.Key.from_path),以及在 GAE 中创建的每个人如何生成唯一 ID,但我仍然不知道如何从如果只显示人名,则在 HTML 中显示的列表。

这似乎是可能的,而且可能很简单,但经过两天的搜索,我什么也没找到。如果我应该提供更多信息,请告诉我,这是我在 Stack Overflow 上的第一篇文章!感谢您的建议。

【问题讨论】:

    标签: python html google-app-engine object select


    【解决方案1】:

    根据documentation

    已存储在数据存储中的每个模型实例都有一个表示对象的唯一键。模型实例的key() 方法返回实例的Key 对象。

    所以你必须传递客户的key不是全名。修复您的模板代码:

    <select size="5" name="client"> 
      {% for person in clients %}
        <option value="{{ person.key }}">
          {{ person.first|escape }} {{ person.last|escape }}
        </option>
      {% endfor %}
    </select>
    

    指出&lt;option&gt; 元素有自己的value 属性。请注意,为什么我将 &lt;select&gt; 元素的名称从 cleint_list 更改为 client 是因为它不能选择多项,而只能选择一项。

    然后,通过其键检索客户端实例。根据文档:

    应用程序可以使用get() 函数检索给定Key 的模型实例。

    所以在你的应用程序中插入以下代码:

    client_key = Key(self.request.get('client'))
    client = Person.get(client_key)
    if client is None:
        # in case the passed key isn't available
        return
    reservation.client = client
    

    【讨论】:

    • 两件事:首先,您可以在选项中使用 ID 或名称(视情况而定)(例如,&lt;option value="{{person.key.id}}"&gt;),这样更紧凑。您可以使用db.Key.from_path('Person', num) 重建密钥。其次,不需要获取Person 只是为了设置引用属性——你可以简单地做reservation.client = client_key
    • @NickJohnson 不,要获取Person 需要进行验证。如果没有,Reservation.client 可以指向一个空引用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多