Running python manage.py check
throws an error AttributeError: 'tuple' object has no attribute 'rsplit'
.
Traceback (most recent call last):
File "/home/mieltn/django_projects/batch/manage.py", line 22, in <module>
main()
File "/home/mieltn/django_projects/batch/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/mieltn/.virtualenvs/django3/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/home/mieltn/.virtualenvs/django3/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
django.setup()
File "/home/mieltn/.virtualenvs/django3/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/mieltn/.virtualenvs/django3/lib/python3.9/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/mieltn/.virtualenvs/django3/lib/python3.9/site-packages/django/apps/config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 855, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/mieltn/django_projects/batch/unesco/models.py", line 4, in <module>
class Category(models.Model):
File "/home/mieltn/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/base.py", line 320, in __new__
new_class._prepare()
File "/home/mieltn/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/base.py", line 333, in _prepare
opts._prepare(cls)
File "/home/mieltn/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/options.py", line 285, in _prepare
pk_class = self._get_default_pk_class()
File "/home/mieltn/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/options.py", line 238, in _get_default_pk_class
pk_class = import_string(pk_class_path)
File "/home/mieltn/.virtualenvs/django3/lib/python3.9/site-packages/django/utils/module_loading.py", line 13, in import_string
module_path, class_name = dotted_path.rsplit('.', 1)
AttributeError: 'tuple' object has no attribute 'rsplit'
I saw similar thread about such error and it doesn't seem to be the case. The problem is supposedly connected with models.py, because when the models file remains empty everything works fine. When I add
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=128)
check crashes.
I guess, there might be a silly mistake or mistype, I'm failing to capture. Thanks in advance.
EDIT: settings.py looks as following
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-_w_1zo)mcg((=iko!2-1vqjf)dme9#la0rm=q2x5oe@c91%&!4'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'unesco.apps.UnescoConfig',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'batch.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'batch.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
EDIT 2: unesco/apps.py has only UnescoConfig
from django.apps import AppConfig
class UnescoConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField',
name = 'unesco'
CodePudding user response:
Django provides DEFAULT_AUTO_FIELD which is a default primary key field type to use for models that don’t have a field with primary_key=True
. This can also be override on the app level using
AppConfig.default_auto_field
. Since you override default_auto_field
field on the app level it gets priority compared to settings.DEFAULT_AUTO_FIELD
.
Django then try to import this string dynamically. As part of it, it then try to split the string into module_path
and class_name
.
So answering your question, the issue is here
from django.apps import AppConfig class UnescoConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField', name = 'unesco'
This will treat default_auto_field
as tuple(Note the trailing comma) instead of a string.
>>> myfield = 'django.db.models.BigAutoField',
>>> type(myfield)
<class 'tuple'>
>>> module_path, class_name = myfield.rsplit('.', 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'rsplit'