TypeError:__ init __()得到一个意外的关键字参数'parent'

时间:2016-05-25 09:38:36

标签: python python-2.7 google-app-engine google-cloud-datastore app-engine-ndb

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

我的模型基类

来自google.appengine.ext import 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'

1 个答案:

答案 0 :(得分:2)

这里有很多错误。您的主要原因是您已覆盖__init__方法,因此它不接受任何参数,但您尝试使用参数将其命名为。显然,您的子类方法应该接受与基类相同的参数,并将这些参数传递给super调用 - 通常用*args, **kwargs完成。

然而,看起来你应该完全覆盖__init__。您重写的方法中唯一的代码绝对不属于那里。字段必须始终在类级别定义;如果在方法中定义它们,它们将无法工作。将这些字段移出,并完全删除该方法。

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