I am getting __init __ () takes at least 2 arguments (1 given) on an IntegerField

These are my models.py I don't have enough arguments in init def

I know there are many similar questions, but I cannot find a 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 get

TypeError: Couldn't reconstruct field attack on highscores.Skills: __init__() takes at least 2 arguments (1 given)

      

Not sure what is wrong.

This is a complete 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)

      

+3


source to share


1 answer


Remove the positional argument exp_field

from the constructor signature and get the field from the kwargs

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)

      

+2


source







All Articles