Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python error AttributeError: 'str' object has no attribute 'setdefault'

Tags:

python

django

I'm trying to run the django project using this command.

python manage.py runserver 8080

But everytime I'm trying to run I faced the such a error.

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/core/management/__init__.py", line 327, in execute
django.setup()
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in    import_module
__import__(name)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/db/models/base.py", line 108, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/db/models/base.py", line 307, in add_to_class
value.contribute_to_class(cls, name)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/db/models/options.py", line 263, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/db/utils.py", line 209, in __getitem__
self.ensure_defaults(alias)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/db/utils.py", line 181, in ensure_defaults
conn.setdefault('ATOMIC_REQUESTS', False)

AttributeError: 'str' object has no attribute 'setdefault'

I tried python2(python2.7.11) and python3(python3.5.1) with virtualenvwrapper. I think it's not the bug of the project source. but something missed in environment configuration. But I can't figure out what the problem is. Please help me fix it.

Thanks in advance.

like image 552
Softalent Avatar asked Aug 30 '16 11:08

Softalent


1 Answers

Each element in the DATABASES dict must itself be a dict. You have overwritten the 'default' entry to just be a string.

Since you are using sqlite3 and the default database name, you should just revert to the original version of the setting:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
like image 153
Daniel Roseman Avatar answered Oct 24 '22 01:10

Daniel Roseman