Django app NameError however the app is installed - python

I'm trying to modify Django's built-in authetnication system by adding a custom user model. The customized model is defined inside an app named accounts:
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.conf import settings
# Create your models here.
COUNTRIES = settings.COUNTRY_CHOICES
class CustomUser(AbstractUser):
zip_code = models.PositiveSmallIntegerField(blank=True, null=True)
city = models.CharField(blank=True, null=True, max_length=64)
address_street = models.CharField(blank=True, null=True, max_length=64)
address_number = models.CharField(blank=True, null=True, max_length=32)
country = models.CharField(blank=True, null=True, choices=COUNTRIES, max_length=8)
I updated the settings.py file this way:
AUTH_USER_MODEL = accounts.CustomUser
I added accounts as an installed app:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
'store',
'cart',
]
When I try to run the python manage.py makemigrations command in terminal to apply changes made to the user model Django's throwing this error:
Traceback (most recent call last):
File "C:\Users\uhlar\Dev\ecommerce\manage.py", line 22, in <module>
main()
File "C:\Users\uhlar\Dev\ecommerce\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\core\management\__init__.py", line 386, in execute
settings.INSTALLED_APPS
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\conf\__init__.py", line 87, in __getattr__
self._setup(name)
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\conf\__init__.py", line 74, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\conf\__init__.py", line 183, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\uhlar\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\uhlar\Dev\ecommerce\config\settings.py", line 149, in <module>
AUTH_USER_MODEL = accounts.CustomUser
NameError: name 'accounts' is not defined
The error is not only confined to the accounts app, no matter inside which app I try to define the CustomUser model I'm still getting it.
If anyone knows what's causing the problem please help me to resolve it.

The AUTH_USER_MODEL setting [Django-doc] should be a string, not a (qualified) name, so:
# settings.py
# …
# string literal 🖟 🖟
AUTH_USER_MODEL = 'accounts.CustomUser'

As Willem mentioned, you need to update your AUTH_USER_MODEL setting inside settings.py to accounts.CustomerUser model. In addition, you will have to update your schema manually as when you first run migrations, Django will use the default auth.User for multiple foreign keys etc. See the docs here for changing a user mid project.

Related

Problem with Django not finding a module 'learning_logs'

I've been trying to fix this Django issue. I've just started this project and I'm fairly new to this language so I don't know where exactly is the issue.
Error:
Traceback (most recent call last):
File "C:\Users\user\Desktop\Projects Python\Django\manage.py", line 22, in <module>
main()
File "C:\Users\user\Desktop\Projects Python\Django\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\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 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'learning_logs'
models.py:
from django.db import models
# Create your models here.
class Topic(models.Model):
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.text
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'learning_logs',
]
Any help I would really appreciate it.
In settings.py, the Installed apps should be written as 'app_name.apps.appconfig'.
The 'appconfig' is the same as the class name in app/apps.py
In you case it might be something like learning_logs.apps.LearningLogsConfig

Getting an Error when running makemigrate on Product model fields that have been added to an existing model

Admittedly I’m a newbie to django and I’m getting the following error when trying to run python manage.py makemigrations
I’m trying to add a new field in to my class Product(models.Model):
featured = models.BooleanField()
Code
from django.db import models
# Create your models here.
class Product(models.Model):
title = models.CharField(max_length=120) # max_length = required
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=10000)
summary = models.TextField()
featured = models.BooleenField() #only new added line
Error: AttributeError: module 'django.db.models' has no attribute 'BooleenField'
command:
python manage.py makemigrations <enter>
entire_traceback:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\.....\trydjango\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\.....\trydjango\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\.....\trydjango\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\.....\trydjango\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\.....\trydjango\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\.....\trydjango\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\.....\src\products\models.py", line 4, in <module>
class Product(models.Model):
File "C:\.....\src\products\models.py", line 9, in Product
featured = models.BooleenField()
AttributeError: module 'django.db.models' has no attribute 'BooleenField'
There is an error in your code for the Product models.
I have corrected it.
from django.db import models
class Product(models.Model):
title = models.CharField(max_length=120) # max_length = required
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=10000)
summary = models.TextField()
featured = models.BooleanField() #only new added line
the default value for a boolean field is always false.

Django1.10 Error: django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

I am using Django 1.10, and trying to use the django-dynamic-scraper package following the tutorial: http://django-dynamic-scraper.readthedocs.io/en/latest/getting_started.html
I have encountered the problem when I was calling "python manage.py makemigrations" :RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
The full version is:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
self.check()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 385, in check
include_deployment_checks=include_deployment_checks,
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 372, in _run_checks
return checks.run_checks(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/urls/resolvers.py", line 310, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/urls/resolvers.py", line 303, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/zhangjintao/WorkingPlace/dds/dds/urls.py", line 20, in <module>
url(r'^admin/', admin.site.urls),
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 267, in urls
return self.get_urls(), 'admin', self.name
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 225, in get_urls
from django.contrib.contenttypes import views as contenttype_views
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/contenttypes/views.py", line 5, in <module>
from django.contrib.contenttypes.models import ContentType
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/contenttypes/models.py", line 138, in <module>
class ContentType(models.Model):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py", line 113, in __new__
"INSTALLED_APPS." % (module, name)
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
But my installed apps contain the django.contrib.contenttypes :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'dynamic_scraper.models',
]
My models.py:
from django.db import models
from dynamic_scraper.models import Scraper, SchedulerRuntime
from scrapy_djangoitem import DjangoItem
class NewsWebsite(models.Model):
name = models.CharField(max_length=200)
url = models.URLField()
scraper = models.ForeignKey(Scraper, blank=True, null=True, on_delete=models.SET_NULL)
scraper_runtime = models.ForeignKey(SchedulerRuntime, blank=True, null=True, on_delete=models.SET_NULL)
def __unicode__(self):
return self.name
class Article(models.Model):
title = models.CharField(max_length=200)
news_website = models.ForeignKey(NewsWebsite)
description = models.TextField(blank=True)
url = models.URLField()
checker_runtime = models.ForeignKey(SchedulerRuntime, blank=True, null=True, on_delete=models.SET_NULL)
def __unicode__(self):
return self.title
class ArticleItem(DjangoItem):
django_model = Article
My admin.py :
from django.contrib import admin
from .models import Article, NewsWebsite, ArticleItem
# Register your models here.
#admin.register(Article, ArticleItem, NewsWebsite)
class ArticleAdmin(admin.ModelAdmin):
pass
class NewWebsiteAdmin(admin.ModelAdmin):
pass
class ArticleItemAdmin(admin.ModelAdmin):
pass
The urls.py file:
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
The folder structure looks like this:
I'm confused what's wrong with my code...
dynamic_scraper section in INSTALLED_APPS should go without .models. e.g.
INSTALLED_APPS = [
...
'dynamic_scraper',
]
Also, since you're using 1.10, it worth to add an AppConfig for your application: docs

Django module object is not iterable

I'm currently trying to build a blog using Django. I've been facing this error for a few hours now. I'm not quite sure if this has anything to do with the directories but the error occurs when I try to register my model in the admin.py file.
from django.contrib import admin
from .models import Post
# Register models
admin.site.register(Post)
The directories look as follows:
blog
models
Post
Category
admin.py
settings
static
templates
Trace:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7ffb589a67b8>
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run
autoreload.raise_last_exception()
File "/usr/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "/usr/lib/python3.6/site-packages/django/core/management/__init__.py", line 327, in execute
autoreload.check_errors(django.setup)()
File "/usr/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/lib/python3.6/site-packages/django/apps/registry.py", line 120, in populate
app_config.ready()
File "/usr/lib/python3.6/site-packages/django/contrib/admin/apps.py", line 23, in ready
self.module.autodiscover()
File "/usr/lib/python3.6/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/usr/lib/python3.6/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/hallak/Projects/hallak.io/hallak_blog/admin.py", line 5, in <module>
admin.site.register(Post)
File "/usr/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 102, in register
for model in model_or_iterable:
TypeError: 'module' object is not iterable
Post:
from django.db import models
from django.utils import timezone
class Post(models.Model):
# Auto-generated ID
id = models.AutoField(primary_key=True)
# Title
title = models.CharField(max_length=100)
# Slug
slug = models.SlugField(max_length=100)
# Content
body = models.TextField()
# Timestamps
created_on = models.DateField(auto_now_add=True)
published_on = models.DateTimeField(blank=True, null=True)
# Category
category = models.ForeignKey('.Category', on_delete=models.DO_NOTHING)
# Author
author = models.ForeignKey('auth.User', on_delete=models.DO_NOTHING)
# Publish post
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
Category:
from django.db import models
class Category(models.Model):
# Auto-generated ID
id = models.AutoField(primary_key=True)
# Title
title = models.CharField(max_length=100)
# Slug
slug = models.SlugField(max_length=100)
# Timestamps
created_on = models.DateField(auto_now_add=True)
The error is happening here: https://github.com/django/django/blob/master/django/contrib/admin/sites.py#L100-L101
Whenever I comment the register line everything works fine.
Instead of writing "from .models import Post" you should write "from .models.Post import Post".
First "Post" is a modulename (file name), second one is a class name.

NameError: name 'base' is not defined(haystack search)

I wanted to include full text search in my django application. I am using whoosh-haystack for this.When I include whoosh and haystack in my installed apps,and execute the command ./manage.py, I am getting an import error. Can anyone sort this out.
settings.py
INSTALLED_APPS = {
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'search',
'whoosh',
'haystack',
}
when I make migration in my model the error which I got is:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Samad Talukder\AppData\Local\Programs\Python \Python36\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line
utility.execute()
File "C:\Users\Samad Talukder\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line 303, in execute
settings.INSTALLED_APPS
File "C:\Users\Samad Talukder\AppData\Local\Programs\Python\Python36\lib\site-packages\django\conf\__init__.py", line 48, in __getattr__
self._setup(name)
File "C:\Users\Samad Talukder\AppData\Local\Programs\Python\Python36\lib\site-packages\django\conf\__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\Samad Talukder\AppData\Local\Programs\Python\Python36\lib\site-packages\django\conf\__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\Samad Talukder\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\Samad Talukder\Desktop\django-env\search\search\settings.py", line 80, in <module>
'PATH': os.path.join(base(), 'whoosh_index')
NameError: name 'base' is not defined
my haystack connection:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(base(), 'whoosh_index')
},
}
my models.py:
from django.contrib.auth.models import User
from django.db import models
class Author(User):
pass
class Book(models.Model):
title = models.CharField(max_length=300)
author = models.ForeignKey(Author)
isbn = models.CharField(max_length=300)
resume = models.TextField()
def __unicode__(self):
return self.title
for more information I install haystack and whoosh in my django project by django pip install method like this:
pip install haystack
pip install whoosh
The traceback is quite clear:
File "C:\Users\Samad Talukder\Desktop\django-env\search\search\settings.py", line 80, in <module>
'PATH': os.path.join(base(), 'whoosh_index')
NameError: name 'base' is not defined
This means that line #80 of your settings.py file uses a name (base - obviously expected to be a function) that is not defined. Your settings file is missing either an import or a function definition. What base is supposed to do etc is beyond our knowledge (it's definitly not a builtin, and nothing standard in a django settings file) but you should now since it's your project.

Categories