【问题标题】:Updating Model in Gae datastore server在 Gae 数据存储服务器中更新模型
【发布时间】:2010-11-16 17:50:50
【问题描述】:

我有一个个人资料类:

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

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

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

这在本地主机上完全可以正常工作,但是当我在服务器上更新它时出现此错误 “'NoneType' 对象没有属性 'access'” 有没有人遇到过同样的情况

更新: 发现配置文件类中的一个对象被返回为 None。这是在 localhost 上获取配置文件对象但在服务器上没有获取配置文件对象的代码

 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 希望现在格式没问题。

谢谢你 西克里希纳

【问题讨论】:

  • 如果您的代码被格式化,则更容易阅读和回答您的问题。
  • 能否包含用于读取和更新配置文件实体的代码。您可能还想包含您正在使用的实际代码。您的代码有几个问题,例如“db.keys”和“db.stringProperty”。

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


【解决方案1】:

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

再次感谢您的帮助

【讨论】:

    【解决方案2】:

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

    您需要修改与配置文件实体交互的处理程序代码,以检查是否存在访问权限。 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.
    

    【讨论】:

    • 我想这会解决我的问题,但我所做的是,我使用新模型架构创建了全新的应用程序并更改了 app.yaml 文件中的名称,即使这样它也没有创建新的场地。但是,我在代码中看到了一个小错误。首次使用 profile.Access 代替 profile.access。这会是“访问”字段不在其他地方的原因吗?
    猜你喜欢
    • 2012-06-06
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2018-06-24
    • 2023-03-23
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多