I have an entity with a coloumn name geocode which is initially null value. I want to update the value of the geocode coloumn in the datastore.
I tried using the below snippet of code but it didn't work. Please Help. Thanks in advance.
The Datastore
class doctor(db.Model):
doctorUser=db.UserProperty()
geocode=db.GeoPtProperty()
The function to update the coloumn of doctor
def post(self):
user=users.get_current_user()
q=doctor.all().filter("doctorUser =",user)
if q.count()==1:
lat=self.request.get('lat')
lng=self.request.get('long')
code=str(lat)+", "+str(lng)
q[0].geocode=code
q[0].put()
SOLVED
Here is the change that i made and it worked!
def post(self):
user=users.get_current_user()
q=doctor.all().filter("doctorUser =",user)
qo=q.get()
if q.count()==1:
lat=self.request.get('lat')
lng=self.request.get('long')
code=str(lat)+", "+str(lng)
qo.geocode=code
qo.put()
You are trying to process a query object, you need to either fetch or iterate over the query to or use get() get results. The query that you are using could return more than one result,. unless you have logic elsewhere to ensure unique values for doctorUser.
Also grab your lat/long from the request before you enter the loop.
You should also stop thinking about the datastore and your model in terms of columns names. These are attributes of entities, there are no columns in the appengine datastore.
Whilst your solution you updated your original question might work it is inefficient. For instance you get an entity then do a count() which reruns the same query. I have rewritten it to be a bit more efficient, however there are still potential problems (such as multiple records that match doctorUser = user
def post(self):
user=users.get_current_user()
if user: # if user not logged in user will be None
qo=doctor.all().filter("doctorUser =",user).fetch(10)
if len(go) > 1:
raise ValueError('More than one doctorUser matches')
if go:
lat=self.request.get('lat')
lng=self.request.get('long')
code=str(lat)+", "+str(lng)
qo.geocode=code
qo.put()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With