开发者

Accessing fields in model in post procedure in Google App Engine

开发者 https://www.devze.com 2023-03-07 15:44 出处:网络
I have a post(self) and I want to add some logic here to add lat and lng (these are computed from google maps) to the data store as defined in my db model. Should I add to data, or should I do it some

I have a post(self) and I want to add some logic here to add lat and lng (these are computed from google maps) to the data store as defined in my db model. Should I add to data, or should I do it some other way such as with the original class. What is the best way to do this?

so...

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['resul开发者_如何学JAVAts'][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?

Thanks


The djangoforms help page explains how to add data to your datastore entity. Call save method with commit=False. It returns datastore entity and then you can add fields before saving it with 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()


It really depends on the types of queries you intend to do. If you want to perform geospatial queries, GeoModel is built for your use case.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号