在Gae数据存储区服务器中更新模型

时间:2010-11-16 17:50:51

标签: google-app-engine google-cloud-datastore maven-gae-plugin

我有个人资料类:

class profile(db.Model):
  user = db.stringProperty()
  # Other properties ...
  access = db.ListProperty(db.keys)

class apps(db.Model):
  name = db.StringProperty()

配置文件类在那里安静了一段时间,但我们最近添加了访问字段,它将存储应用程序的密钥。 现在我们在应用程序上添加配置文件权限,访问字段不会在模型中更新。

这完全适用于localhost,但是当我在服务器上更新它时,我收到此错误 “'NoneType'对象没有属性'access'” 有没有人遇到同样的情况

更新 弄清楚,profile类中的一个对象被返回为None。以下是在localhost上获取配置文件对象但在服务器上获取None的代码

 liuser = users.User(request.POST['user']) 
 #request.POST['user'] gets user Gmail ID, which is being converted to user object
 profiles=Profile.all().filter(" user =", liuser).get()
 userprofile=profiles

 #tried below code which returns "'NoneType' object has no attribute 'access'" on server, the same gets a profile object on localhost
 if not hasattr(userprofile, "access"): 
    userprofile.access=[]

@Robert希望现在格式化正常。

谢谢 Sai Krishna

2 个答案:

答案 0 :(得分:1)

我们能够解决这个问题。问题出在users.User对象上,它没有为gmail用户附加@gmail.com,但是它接受了其他具有域名的域,这就是抛出无类型对象

再次感谢您的帮助

答案 1 :(得分:0)

将属性添加到模型时,数据存储区中的模型的现有实例不会自动获取该属性。

您需要修改与配置文件实体交互的处理程序代码,以检查是否存在访问权限。 Python的hasattr函数可以做到。像这样:

a_profile = profile.all().somequerystuffheretogetaninstance().get()
if a_profile is not None:
    if not hasattr(a_profile, "access"):
        a_profile.access = whateveryourdefaultdatais
    # perform my data logic normally after this, but remember to persist
    # this to the datastore to save the new access property.
相关问题