Google App Engine 中的 python - 存在类而不是实例问题,但使用 db.Model 因此无法创建 init 方法
我正在使用 Python 和 GAE 编写一个应用程序来比较产品。这些产品将属于一组相似的产品,应用程序会计算每组中的最佳价值。
当我创建新产品时,可以将其添加到现有产品集中,也可以创建新产品集。
测试应用程序时,第一组创建得很好。我用产品名称填充该集合的一个实例。我使用一个网页上的表单将数据发布到“suppbook”页面。我仍然不清楚网页如何成为一个类,但这是一个不同的问题。
围绕所有这些还有更多代码,但我试图使我的问题尽可能清楚。
class Supp(db.Model):
name = db.StringProperty(multiline=False)
# a bunch of other attributes using Google's DB Model
class SuppSet(db.Model):
name = db.StringProperty(default='')
supp_list = set([])
# a bunch of other attributes using Google's DB Model
# i tried to add this after reading a few questions on SO but GAE doesn't like it
def __init__(self,):
self.name = 'NoName'
self.best_value = 'NoBestValue'
self.supp_list = set([])
Class Suppbook(webapp.RequestHandler):
def post(self):
supp = Supp()
suppSet = SuppSet()
...
supp.name = self.request.get('name')
supp.in_set = self.request.get('newset')
suppSet.name = supp.in_set
suppSet.supp_list.add(supp.name)
self.response.out.write('%s now contains %s<p>' % (suppSet.name,suppSet.supp_list))
第一次效果很好,如果我只使用一个 SuppSet
,我可以向其中添加许多支持。但是,如果我创建另一个 SuppSet
,则两个 suppSet
的 supp_list
将具有相同的内容。我一直在研究这里的问题,我认为(知道)我在类与实例属性访问方面做错了一些事情。我尝试为 SuppSet
创建一个 __init__
方法,但 GAE 抱怨: AttributeError: 'SuppSet' object has no attribute '_entity'
另外,我使用 GAE 数据存储来 put() 和 get() Supps 和 SuppSets,所以我不清楚为什么我不对应该从数据库中提取的唯一实例进行操作。
我不确定我是否提供了足够的信息,但我想开始解决这个问题。如果需要更多信息来帮助调试,请告诉我。
我也承认我的做法是完全错误的。我正在考虑重写整个内容,但我已经接近“完成”基本功能,因此我想尝试解决这个问题。
谢谢
i am writing an app to compare products, using Python and GAE. The products will belong to a set of similar products, and the app calculates the best value in each set.
When i create a new product, it can be added to an existing set or a new set can be created.
When testing the app, the first set gets created just fine. I populate an instance of the set with the name of the product. I use a form on one web page to POST the data into the "suppbook" page. I'm still not clear on how a web page can be a class but that's a different question.
There's more code around all of this but I'm trying to make my question as clear as possible.
class Supp(db.Model):
name = db.StringProperty(multiline=False)
# a bunch of other attributes using Google's DB Model
class SuppSet(db.Model):
name = db.StringProperty(default='')
supp_list = set([])
# a bunch of other attributes using Google's DB Model
# i tried to add this after reading a few questions on SO but GAE doesn't like it
def __init__(self,):
self.name = 'NoName'
self.best_value = 'NoBestValue'
self.supp_list = set([])
Class Suppbook(webapp.RequestHandler):
def post(self):
supp = Supp()
suppSet = SuppSet()
...
supp.name = self.request.get('name')
supp.in_set = self.request.get('newset')
suppSet.name = supp.in_set
suppSet.supp_list.add(supp.name)
self.response.out.write('%s now contains %s<p>' % (suppSet.name,suppSet.supp_list))
This works well the first time around, and if I only use one SuppSet
, I can add many supps to it. If I create another SuppSet
, though, both suppSets
will have the same contents for their supp_list
. I have been looking through the questions on here and I think (know) I'm doing something wrong regarding class vs. instance attribute access. I tried to create an __init__
method for SuppSet
but GAE complained: AttributeError: 'SuppSet' object has no attribute '_entity'
Also, I am using the GAE datastore to put() and get() the Supps and SuppSets, so I'm not clear why I'm not acting on the unique instances that I should be pulling from the DB.
I am not sure if I am providing enough information but I wanted to get started on this issue. Please let me know if more info is needed to help debug this.
I'm also open to the idea that i'm going about this completely wrong. I'm considering re-writing the whole thing, but I'm so close to being "finished" with basic functionality that I'd like to try to solve this issue.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在你的 init 中,你需要调用超级的 init,db.Model 在它的 init 中有一些重要的事情要做,你必须与签名相符。
但是,您可能不应该在那里设置默认值之类的东西。尝试仅使用数据存储属性功能来设置默认值。
In your init you will need to call the super's init, db.Model has some important stuff to do in its init, you will have to match the signature.
However you likely shouldn't be setting up things like defaults in there. Try and just use the datastore Properties ability to set a default.
您的代码中有一些(我认为)拼写错误。 Python 对大小写和空格敏感。您使用的属性名称也与您的定义不匹配,例如
in_set
。如果可能,请发布实际的工作示例来证明您的问题。You've got some (I assume) typos in your code. Python is sensitive to case and white-space. The attribute names you use also don't match your defs, such as
in_set
. When possible, post actual working examples demonstrating your problem.