Search code examples
pythondjangopython-3.xpython-3.6django-1.11

retrieving Django object returns AttributeError: module 'types' has no attribute 'StringTypes'


I'm attempting to migrate my apps from py2 to py3, and I'm running into this error when running migration scripts. After some digging, I get the same error when doing MyModel.objects.get(id=<some_id>)

Now, I know StrginType is obsolete in python3 and I removed all basestring and replace with string in my scripts (not sure if related), but I'm not sure where this error is being triggered. I'm running Django 1.11 so it should be compatible.

It seems to be it's something about Django but I can't figure out what. I searched on django doc and got nothing.

Anyone got the same error before when retrieving an object through django?

EDIT:

full traceback

Traceback (most recent call last):
  File "manage.py", line 45, in <module>
    main()
  File "manage.py", line 41, in main
    execute_from_command_line(sys.argv)
  File "<some_domain>/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "<some_domain>/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "<some_domain>/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "<some_domain>/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "<some_domain>/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "<some_domain>/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "<some_domain>/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "<some_domain>/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "<some_domain>/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "<some_domain>/lib/python3.6/site-packages/django/db/migrations/operations/special.py", line 193, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "<some_domain>/<path_to_migration>/migrations/<migration file>.py", line 10, in <some module>
    <some model name> = apps.get_model(<someapp>, <some_model>)
  File "<some_domain>/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __iter__
    self._fetch_all()
  File "<some_domain>/lib/python3.6/site-packages/django/db/models/query.py", line 1102, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "<some_domain>/lib/python3.6/site-packages/django/db/models/query.py", line 62, in __iter__
    for row in compiler.results_iter(results):
  File "<some_domain>/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 834, in results_iter
    row = self.apply_converters(row, converters)
  File "<some_domain>/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 819, in apply_converters
    value = converter(value, expression, self.connection, self.query.context)
  File "<some_domain>/lib/python3.6/site-packages/encrypted_fields/fields.py", line 160, in from_db_value
    return self.to_python(value)
  File "<some_domain>/lib/python3.6/site-packages/encrypted_fields/fields.py", line 163, in to_python
    if value is None or not isinstance(value, types.StringTypes):
AttributeError: module 'types' has no attribute 'StringTypes'

Solution

  • You are using a library, "encrypted-fields", that is not compatible with Python 3. Looking at the repo for that project, it has not been updated in more than three years, so appears to be abandoned.

    There are several alternatives - a quick Google found encrypted-model-fields which might work for you.