在googleappengin的post过程中访问模型中的字段

2024-09-28 21:58:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个post(self),我想在这里添加一些逻辑,将lat和lng(它们是从google地图计算出来的)添加到数据库模型中定义的数据存储中。我应该添加到数据中,还是应该以其他方式(例如使用原始类)添加数据。最好的方法是什么?你知道吗

所以。。。你知道吗

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?

谢谢


Tags: 数据selftruedbdatarequiredformspost
2条回答

djangoforms help page解释了如何向数据存储实体添加数据。用commit=False调用save方法。它返回数据存储实体,然后您可以在使用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()

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

相关问题 更多 >