【问题标题】:Attribute auto_now_add not working properly on google endpoints属性 auto_now_add 在谷歌端点上无法正常工作
【发布时间】:2017-11-27 09:47:12
【问题描述】:

我正在使用谷歌应用引擎端点来创建 API,因此我使用 updatedDate auto_now 和 createdDate auto_now_add 的预定义属性设计了我的模型。 updatedDate 列已正确填写,但 createdDate 始终为 None。

这是我用来持久化实体的代码:

  @endpoints.method(PartyForm, PartyForm, path='parties', http_method='POST', name='insert')
  def insert(self, request):
    data = {field.name: getattr(request, field.name) for field in request.all_fields()}

    if data['launchDate']:
      data['launchDate'] = datetime.strptime(data['launchDate'][:10], "%Y-%m-%d").date()

    p_id = Party.allocate_ids(size=1)[0]
    p_key = ndb.Key(Party, p_id)
    data['key'] = p_key

    Party(**data).put()
    return request

【问题讨论】:

    标签: python google-app-engine google-cloud-platform google-app-engine-python


    【解决方案1】:

    几个小时后,我找到了auto_now_add 属性不起作用的原因。在我的模型文件中,我创建了两个类:

    • Party(ndb.Model): 这是ndb模型
    • PartyForm(messages.Message):这是端点表单类模型

    错误是在我的端点表单类模型PartyForm 中,我添加了属性createdDate 和updatedDate。由于某种原因,updatedDate 有效,但 createdDate 无效。在我从表单类模型中删除此属性后,一切正常。

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 2016-09-28
      • 2018-01-02
      • 2011-03-25
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多