属性 auto_now_add 在 google 端点上无法正常工作

Attribute auto_now_add not working properly on google endpoints

我正在使用 google 应用引擎端点来创建 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

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

  • Party(ndb.Model): 这是ndb模型
  • PartyForm(messages.Message):这是端点形式class模型

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