gpt4 book ai didi

python - 类型错误 : __init__() got an unexpected keyword argument 'parent'

转载 作者:行者123 更新时间:2023-12-05 04:13:31 28 4
gpt4 key购买 nike

我正在使用 Cloud Datastore 后端在 Google App Engine 中构建应用程序。

我的模型基类

从 google.appengine.ext 导入 ndb

class CFCModel(ndb.Model):
"""
Base model from which we can inherit other models. Test
"""
created_date = ndb.DateTimeProperty(auto_now=True)
modified_date = ndb.DateTimeProperty(auto_now_add=True)

模型继承的类是

class PraiseReport(CFCModel):
def __init__(self):
self.username = ndb.StringProperty(repeated=True)
self.report = ndb.StringProperty(required=True)
super(PraiseReport, self).__init__()

我现在正在尝试创建一个对象

key = PraiseReportRepository.get_praise_report_key()
praise_report_model = PraiseReport(parent=key)
praise_report_model.username = praise_report.username
praise_report_model.report = praise_report.report
praise_report_model.put()

我得到以下错误

ERROR    2016-05-25 09:34:59,882 app.py:1423] Exception on /forms/add_praise_report [GET]
Traceback (most recent call last):
File "/Users/vinay/App-Engine/CM-12/server/lib/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/vinay/App-Engine/CM-12/server/lib/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/vinay/App-Engine/CM-12/server/lib/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/vinay/App-Engine/CM-12/server/lib/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/vinay/App-Engine/CM-12/server/lib/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/vinay/App-Engine/CM-12/cfc/decorators/authentication.py", line 62, in authenticate_and_call_member
return func(*args, **kwargs)
File "/Users/vinay/App-Engine/CM-12/Routes/PraiseReportRoutes.py", line 34, in add_report
repo.add(report)
File "/Users/vinay/App-Engine/CM-12/cfc/data/PraiseReportRepository.py", line 12, in add
praise_report_model = PraiseReport(parent=key)
TypeError: __init__() got an unexpected keyword argument 'parent'

最佳答案

这里有不少错误。您的主要问题是您已经重写了 __init__ 方法,因此它不接受任何参数,但是您试图用 参数调用它。显然,您的子类方法应该接受与基类相同的参数,并将这些参数传递给 super 调用——通常是通过 *args, **kwargs 完成的。

但是,看起来您根本不应该在这里覆盖 __init__。你在那个被覆盖的方法中拥有的唯一代码绝对不属于那里。字段必须始终在类级别定义;如果您在方法中定义它们,它们将根本不起作用。将这些字段移出,并完全删除该方法。

class PraiseReport(CFCModel):
username = ndb.StringProperty(repeated=True)
report = ndb.StringProperty(required=True)

关于python - 类型错误 : __init__() got an unexpected keyword argument 'parent' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37433219/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com