سؤال

I recently switched from ext.db to NDB.model on Google App Engine and still fairly new to database in general. Below is the new NDB code, which is mostly unchanged from ext.db.

What I want to do is when a user tries to login, check if uid exists. If so, pass back the data from database as "entry." If uid does not exist, then pull the data from Json and populate the new "entry."

class User(ndb.Model):
    uid             = ndb.StringProperty(required = True)
    firstName       = ndb.StringProperty(required = True)
    lastName        = ndb.StringProperty(required = True)
    emailAddress    = ndb.StringProperty(required = True)
    password        = ndb.StringProperty(required = True)
    created         = ndb.DateTimeProperty(auto_now_add = True)
    last_modified   = ndb.DateTimeProperty(auto_now = True)

    @classmethod
    def addUser(cls, info):
        entry = User.query().filter(Profile.uid == info['uid']).get        
        #USED TO BE FOR DB: entry = User.all().filter('uid = ', info['uid']).get()
        # if the profile doesn't exist, create it with basic information
        if entry == None:
            entry = User(uid = info["id"], 
                     firstName = info["firstName"], 
                     lastName = info["lastName"], 
                     emailAddress = info["emailAddress"], 
                     password = info[passwd])
        # At this point, we have a valid entry.
        entry.last_modified = datetime.now()
        entry.put()
        return entry

This code does NOT work. I have tried entry = User.query().filter(Profile.uid == info['uid']).get but I get an attribute error

AttributeError: 'instancemethod' object has no attribute 'last_modified'

I apologize for unable to clarify more, but am stuck. What am I missing? Everything worked fine with ext.db (note where I changed as noted in the code above), but I cannot see what is wrong with NDB. I am seeking improvements as well, so please suggest changes.

Any help is greatly appreciated! Thank you.

هل كانت مفيدة؟

المحلول

If was you I will use :

entry = User.query(Profile.uid == info['uid']).get()

Instead of :

entry = User.query().filter(Profile.uid == info['uid']).get

You can specify your filters directly inside query declaration separated by coma. Don't forget parenthesis at the end else you make a function pointer.

نصائح أخرى

This line is your problem

entry = User.query().filter(Profile.uid == info['uid']).get  

You are not calling the method, just assigning it to entry.

@ndb.transactional
def my_get_or_insert(cls, id, **kwds):
  entry = cls.query().filter(Profile.uid == info['uid']).get  
  if not entry:
    entry = cls(**kwds)
  entry.last_modified = datetime.now()
  entry.put()
  return (entry, True)  # True meaning "created"

Transactional - to avoid dirty db operation

kwrds =  {'uid': info["id"], 
                     'firstName' : info["firstName"], 
                     'lastName' : info["lastName"], .....

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top