python - How add data on database from ModelView class flask_admin? -


i use flask_admin in site of admin application. , type address. after that, address doing request google maps , take coordinates: lat, lng. should save coordinates on database? code on flask_admin view

class eventadmin(sqla.modelview): column_display_pk = true la = '' lo = '' form_columns = ['title', 'description', 'date', 'cover', 'city', 'category', 'address'] form_extra_fields = {     'cover': form.imageuploadfield('cover',                                    namegen=prefix_name,                                    base_path=get_path('events'),                                    thumbnail_size=(250, 250, true),                                    allowed_extensions=app.config['allowed_extensions']) }  column_exclude_list = ('longi', 'lati') def on_model_change(self, form, model, is_created):     address = unicode(model)     url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address.encode('utf-8')     response = urllib2.urlopen(url)     jsongeocode = response.read().encode('utf-8')     data = eval(jsongeocode)     coordinates = data['results'][0]['geometry']['location']     self.la = coordinates['lat']     self.lo = coordinates['lng'] 

'la' , 'lo' properties not bound model class not persisted database. instead, should bind response (longitude , latitude) google api model class properties (which have named 'longi' , 'lati'. hide them user with

column_exclude_list = ('longi', 'lati') 

because don't want user enter data (you doing him). when user submits form, on_model_change method gets model instance user submited (with values title, description, date ...) . has add longi , lati values.
instead should have

def on_model_change(self, form, model, is_created):     address = unicode(model)     url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address.encode('utf-8')     response = urllib2.urlopen(url)     jsongeocode = response.read().encode('utf-8')     data = eval(jsongeocode)     coordinates = data['results'][0]['geometry']['location']     model.lati = coordinates['lat']     model.longi = coordinates['lng'] 

Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -