简体   繁体   English

在谷歌 appengine 上使用 django 使用数据初始化表单

[英]initialising a form with data using django on google appengine

I have a post handler that uses a 'from google.appengine.ext.db.djangoforms.ModelForm'.我有一个使用“来自 google.appengine.ext.db.djangoforms.ModelForm”的帖子处理程序。 It pulls the existing instance the data base then initialises the form by:它将现有实例拉入数据库,然后通过以下方式初始化表单:

  myForm = TestForm(instance=self_instance, data=post_data)

I'm only putting one of the three properties in the post_data which is being correctly copied to the form but the other two properties which already have values in the 'self_instance' are being set to None.我只将三个属性之一放在 post_data 中,该属性已正确复制到表单中,但其他两个在“self_instance”中已有值的属性被设置为无。 Can this be avoided?这可以避免吗?

Thanks, Richard谢谢,理查德

This is giving me the behaviour I'm after:这给了我我所追求的行为:

if self_instance:
  logging.info('creating form with instance so updating the post data, we need this to bind the form so we can validate it')
  if post_data:
    #update the post data with existing values
    for prop in self_instance._properties:
      if prop not in post_data:
        cur_prop_val = getattr(self_instance, prop)
        if isinstance(cur_prop_val,db.Model):
          str(cur_prop_val.key())
        else:
          post_data[prop] = cur_prop_val
  else:
    logging.info('no post data so not adding it to form')
  form_data = form_class(data=post_data,instance=self_instance)
else:
  logging.info('creating form without instance')
  form_data = form_class(data=post_data)  

You don't show your form definition.您没有显示您的表单定义。 But, generally, if you don't want to include fields in the form, you should specify them in the Meta exclude tuple - then they won't be overwritten by non-existent POST data.但是,一般来说,如果您不想在表单中包含字段,您应该在 Meta exclude元组中指定它们 - 这样它们就不会被不存在的 POST 数据覆盖。

  1. If you supply instance arg, the form is considered unbound如果您提供instance arg,则该表单被视为未绑定
  2. If you supply data arg, the form is considered bound如果您提供data arg,则该表单被视为已绑定

If you supply both, data will override instance (=> same as case 2, form bound).如果两者都提供, data将覆盖instance (=> 与案例 2 相同,表单绑定)。

So in your case, it's better to load self_instance then update it with data and then use the unbound form:所以在你的情况下,最好加载self_instance然后用data更新它,然后使用未绑定的表单:

myForm = TestForm(instance=self_instance)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在发动机上使用带有django的GeoIP - using GeoIP with django on appengine django / google appengine上的文字差异 - text diff on django/google appengine 将pytest与Google AppEngine一起使用 - Using pytest with Google AppEngine 如何在Google AppEngine中汇总数据 - How to summarise data in Google AppEngine 可以在Google AppEngine上使用django Piston吗? - Is it possible to use django Piston on Google AppEngine? 使用google appengine在django-nonrel中保存实体 - Saving entities in django-nonrel with google appengine 在webapp2中读取表单文件-google appengine - read form file in webapp2 -google appengine 使用WSGIRequest时,未在Google App Engine Django 1.5.11上填充X-AppEngine-CityLatLong - X-AppEngine-CityLatLong not populated on Google App Engine Django 1.5.11 when using WSGIRequest 如何将Appengine与来自API的Python脚本流数据一起使用,将数据流传输到Google Cloud BigQuery? - How to stream data into Google Cloud BigQuery using Appengine with Python Script-flowing data from API? 使用 appengine “django.core.exceptions.ImproperlyConfigured:设置 SECRET_KEY 环境变量”在 Google 云上部署 Django 应用程序时出错 - Error in deploying Django app on Google cloud using appengine "django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM