本文介绍了我得到__init __()在IntegerField上至少有两个参数(1个给定)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的models.py
我在 init 中获得的参数不足
我知道有许多类似的问题像这样,但我找不到解决方案。
类ExpField(models.FloatField):
def __init __(self,* args,** kwargs):
#将默认的default设置为0.
如果kwargs.get('default')为无:
kwargs ['default'] = 0
super(ExpField,self).__ init __(* args,** kwargs)
class LevelField
def __init __(self,exp_field,* args,** kwargs):
#默认的默认设置为1.
如果kwargs。 get('default')为无:
kwargs ['default'] = 1
self.exp_field = exp_field
super(LevelField,self) (* args,** kwargs)
类技能(models.Model):
attack_exp = ExpField()
attack = LevelF ield(exp_field = attack_exp)
我正在获得
TypeError:无法重构高分辨率的字段攻击。技能:__init __()至少需要2个参数(1)
/ pre>
不知道是什么错误。
这是完整的跟踪
[22/04/15 05:31:12] [Raghav's:Runescape] $ python manage.py makemigrations
追溯(最近的呼叫最后):
文件manage.py,第10行,< module>
execute_from_command_line(sys.argv)
文件/Library/Python/2.7/site-packages/django/core/management/__init__.py,第338行,在execute_from_command_line
utility.execute ()
文件/Library/Python/2.7/site-packages/django/core/management/__init__.py,第330行,执行
self.fetch_command(子命令).run_from_argv(self。 argv)
文件/Library/Python/2.7/site-packages/django/core/management/base.py,第390行,在run_from_argv
self.execute(* args,** cmd_options)
文件/Library/Python/2.7/site-packages/django/core/management/base.py,第441行,执行
output = self.handle(* args,** options)
文件/Library/Python/2.7/site-packages/django/core/management/commands/makemigrations.py,第99行,处理
ProjectState.from_apps(apps),
文件/Library/Python/2.7/site-packages/django/db/migrations/state.py,第166行,from_apps
model_state = ModelState.from_model(model)
文件/ Library / PY thon / 2.7 / site-packages / django / db / migrations / state.py,第343行,from_model
e,
TypeError:无法重构highscores.Szills:__init __()至少有两个参数(1个)
解决方案
exp_field
参数,从构造函数的签名中获取字段,从kwargs
dict:class LevelField(models.IntegerField):
def __init __(self,* args,** kwargs):
if kwargs .get('default')为无:
kwargs ['default'] = 1
self.exp_field = kwargs.pop('exp_field',无)
super(LevelField,self) .__ init __(* args,** kwargs)
This is my models.pyI'm getting a argument not enough in init def
I know there are many similar questions like this but I can't find the solution there.
class ExpField(models.FloatField): def __init__(self, *args, **kwargs): # Have a default "default" set to 0. if kwargs.get('default') is None: kwargs['default'] = 0 super(ExpField, self).__init__(*args, **kwargs) class LevelField(models.IntegerField): def __init__(self, exp_field, *args, **kwargs): # Have a default "default" set to 1. if kwargs.get('default') is None: kwargs['default'] = 1 self.exp_field = exp_field super(LevelField, self).__init__(*args, **kwargs) class Skills(models.Model): attack_exp = ExpField() attack = LevelField(exp_field=attack_exp)
I'm getting
TypeError: Couldn't reconstruct field attack on highscores.Skills: __init__() takes at least 2 arguments (1 given)
Not sure what is wrong.
This is the full trace
[22/04/15 05:31:12][Raghav's:Runescape]$ python manage.py makemigrations Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line utility.execute() File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 330, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv self.execute(*args, **cmd_options) File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 441, in execute output = self.handle(*args, **options) File "/Library/Python/2.7/site-packages/django/core/management/commands/makemigrations.py", line 99, in handle ProjectState.from_apps(apps), File "/Library/Python/2.7/site-packages/django/db/migrations/state.py", line 166, in from_apps model_state = ModelState.from_model(model) File "/Library/Python/2.7/site-packages/django/db/migrations/state.py", line 343, in from_model e, TypeError: Couldn't reconstruct field attack on highscores.Skills: __init__() takes at least 2 arguments (1 given)
解决方案Remove the positional
exp_field
argument from the constructor's signature and get the field from thekwargs
dict:class LevelField(models.IntegerField): def __init__(self, *args, **kwargs): if kwargs.get('default') is None: kwargs['default'] = 1 self.exp_field = kwargs.pop('exp_field', None) super(LevelField, self).__init__(*args, **kwargs)
这篇关于我得到__init __()在IntegerField上至少有两个参数(1个给定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!