thanks first!
I met a problem when using custom class,here is the code:
class definition:
先谢谢!我在使用自定义类时遇到了问题,这里是代码:类定义:
class BaseCompetition:
def __init__(self, company):
self.company = company
class SingleLeagueCompetition(Competition):
def __init__(self, company):
BaseCompetition.__init__(self,company)
when using it,like this:
使用它时,像这样:
test_company = Company.objects.get(id=1)
sample_single = SingleLeagueCompetition(test_company)
'Company' is a model.
But I got a error when executing the code,like this:
I just don't know what's wrong...
'公司'是一个典范。但是在执行代码时我遇到了错误,就像这样:我只是不知道什么是错的......
Traceback (most recent call last):
File "/Users/littlep/myWorks/python-works/sports_with_zeal/swz/dao.py", line 32, in __init__
self.company = company
File "/Users/littlep/.pythonbrew/pythons/Python-3.4.3/lib/python3.4/site-packages/django/db/models/fields/related.py", line 639, in __set__
if instance._state.db is None:
AttributeError: 'SingleLeagueCompetition' object has no attribute '_state'
Thanks again!
再次感谢!
1 个解决方案
#1
1
Your SingleLeagueCompetition
class should inherit from BaseCompetition
, like this:
您的SingleLeagueCompetition类应该从BaseCompetition继承,如下所示:
class BaseCompetition:
def __init__(self, company):
self.company = company
class SingleLeagueCompetition(BaseCompetition):
def __init__(self, company):
super().__init__(company)
also instead of call constructor by using parent class BaseCompetition._init_
you can use super
to bind it in child.
通过使用父类BaseCompetition._init_而不是调用构造函数,您可以使用super将其绑定到child中。
More reference consult: https://docs.python.org/3.4/library/functions.html#super
更多参考咨询:https://docs.python.org/3.4/library/functions.html#super
#1
1
Your SingleLeagueCompetition
class should inherit from BaseCompetition
, like this:
您的SingleLeagueCompetition类应该从BaseCompetition继承,如下所示:
class BaseCompetition:
def __init__(self, company):
self.company = company
class SingleLeagueCompetition(BaseCompetition):
def __init__(self, company):
super().__init__(company)
also instead of call constructor by using parent class BaseCompetition._init_
you can use super
to bind it in child.
通过使用父类BaseCompetition._init_而不是调用构造函数,您可以使用super将其绑定到child中。
More reference consult: https://docs.python.org/3.4/library/functions.html#super
更多参考咨询:https://docs.python.org/3.4/library/functions.html#super