Search code examples
pythondjangomodelfieldtype

Django models field types redefine error while syncDB


I am using Django 1.8.3.

I redefine a Field type for some simple restriction.

class BigIntegerField(models.BigIntegerField):
    def __init__(self,  min_value=None, max_value=None, **kwargs):
        validators=[MinValueValidator(min_value), MaxValueValidator(max_value)]
        models.BigIntegerField.__init__(self, validators, **kwargs)



class Test(models.Model):
    a = globalModels.BigIntegerField()

And then I did syncDB but it generates an exception like below.

Operations to perform:
  Synchronize unmigrated apps: staticfiles, models, messages
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.
Traceback (most recent call last):
  File "C:\Users\sangmin\Desktop\modelsTest\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py", line 25, in handle
    call_command("migrate", **options)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 205, in handle
    ProjectState.from_apps(apps),
  File "C:\Python27\lib\site-packages\django\db\migrations\state.py", line 178, in from_apps
    model_state = ModelState.from_model(model)
  File "C:\Python27\lib\site-packages\django\db\migrations\state.py", line 354, in from_model
    e,
TypeError: Couldn't reconstruct field a on models.Test: __init__() got multiple values for keyword argument 'verbose_name'

What is wrong point?

To figure out what probem is, I tried to remove some part in the code.

Finally I found, If I remove validators argument in models.BigIntegerField.__init__(self, validators, **kwargs) It works fine


Solution

  • The first positional argument to any Field class is verbose_name, not validators. You should pass it as a kwarg. (Also, you should really use super()).

    super(BigIntegerField, self).__init__(self, validators=validators, **kwargs)