在Google App Engine中的后续过程中访问模型中的字段

时间:2011-05-19 20:07:46

标签: python google-app-engine

我有一个帖子(自我),我想在这里添加一些逻辑,将lat和lng(这些是从谷歌地图计算)添加到我的数据库模型中定义的数据存储。我应该添加数据,还是应该以其他方式执行此操作,例如使用原始类。这样做的最佳方式是什么?

所以...

class Company(db.Model):
  company_type = db.StringProperty(required=True, choices=["PLC", "LTD", "LLC", "Sole Trader", "Other"])
  company_lat = db.StringProperty(required=True)
  company_lng = db.StringProperty(required=True)

class CompanyForm(djangoforms.ModelForm):
  company_description = forms.CharField(widget=forms.Textarea(attrs={'rows':'2', 'cols':'20'}))
  company_address = forms.CharField(widget=forms.Textarea(attrs={'rows':'2', 'cols':'20'}))

  class Meta:
    model = Company
    exclude = ['company_lat,company_lng']


def post(self):
  data = CompanyForm(data=self.request.POST)
  map_url = ''  
  address = self.request.get("company_postcode")
  ...
  lat = response['results'][0]['geometry']['location']['lat']
  lng = response['results'][0]['geometry']['location']['lng']
  ...
  # How do I add these fields lat and lng to my data store?
  # Should I add them to data? if this is possible?
  # Or shall I do it some other way?

由于

2 个答案:

答案 0 :(得分:2)

djangoforms help page说明了如何向数据存储区实体添加数据。使用save调用commit=False方法。它返回数据存储区实体,然后您可以在使用put()

保存之前添加字段
def post(self):
  ...
  # This code is after the code above
  if data.is_valid():
    entity=data.save(commit=False)
    entity.company_lat=lat
    entity.company_lng=lng
    entity.put()

答案 1 :(得分:1)

这实际上取决于您打算要执行的查询类型。如果您要执行地理空间查询,GeoModel是为您的用例而构建的。