Django: DATABASES IMPROPERLY CONFIGURED, Please supply engine value. (Multiple databases) - python

Hey guys I am trying to have 2 postgres databases in my django project, one to hold user data and the other to hold other contents, but I am getting this error when I try to run createsuperuser but I was able to run both makemigrations and migrate --database=users_db and the same for other db as well.
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
From the django documentation, they have mentioned that it is okay to leave the default dict blank.
If the concept of a default database doesn’t make sense in the context of your project, you need to be careful to always specify the database that you want to use. Django requires that a default database entry be defined, but the parameters dictionary can be left blank if it will not be used. To do this, you must set up DATABASE_ROUTERS for all of your apps’ models, including those in any contrib and third-party apps you’re using, so that no queries are routed to the default database.
This is settings.py
DATABASES = {
'default': {},
'users_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'users_db',
'USER': 'postgres',
'PASSWORD': 'Trial_user123',
'HOST':'127.0.0.1',
'PORT':'5432',
},
'content_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'content_II',
'USER': 'postgres',
'PASSWORD': 'Trial_user123',
'HOST':'127.0.0.1',
'PORT':'5432',
},
}
DATABASE_ROUTERS = ['personal.routers.db_routers.AuthRouter', 'personal.routers.db_routers.PersonalDBRouter', ]
This is the db_routers.py
class AuthRouter:
route_app_labels = {'sessions', 'auth', 'contenttypes', 'admin', 'accounts'}
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'users_db'
return None
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'users_db'
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return 'users_db'
return None
class PersonalDBRouter:
route_app_labels = {'actions', 'blog', 'token_blacklist', 'taggit', 'django_celery_beat', 'django_celery_results',}
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'content_db'
return None
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'content_db'
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return 'content_db'
return None
This is Account Manager and when I try to run createsuperuser, it shows an error in the line where create_user saves.
class MyAccountManager(BaseUserManager):
def create_user(self, email, username, first_name, last_name, password=None):
if not email:
raise ValueError('Users must have an email address')
if not username:
raise ValueError('Users must have a username')
if not first_name:
raise ValueError('Users must have a First Name') # check later
user = self.model(
email=self.normalize_email(email),
username=username,
first_name=first_name,
last_name=last_name,
)
user.set_password(password)
user.save(using="users_db")
return user
def create_superuser(self, email, username, first_name, last_name, password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
username=username,
first_name=first_name,
last_name=last_name,
)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
I don't understand what's causing this error as I have already provided the engine value and cannot find the error. Can someone tell me where the issue is?
Thanks
UPDATE
TRACEBACK:
(myvenv) C:\Demo_1\mainsite>python manage.py createsuperuser --
database=users_db
Email: admin#test.com
Username: admin
First name: Admin
Last name: Test
Password:
Password (again):
Traceback (most recent call last):
File "C:\Demo_1\mainsite\manage.py", line 21, in <module>
main()
File "C:\Demo_1\mainsite\manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
return super().execute(*args, **options)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 398, in execute
output = self.handle(*args, **options)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 189, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
File "C:\Demo_1\mainsite\accounts\models.py", line 98, in create_superuser
user = self.create_user(
File "C:\Demo_1\mainsite\accounts\models.py", line 94, in create_user
user.save()
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\base_user.py", line 67, in save
super().save(*args, **kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 763, in save_base
updated = self._save_table(
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
return manager._insert(
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1414, in execute_sql
with self.connection.cursor() as cursor:
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 259, in cursor
return self._cursor()
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\dummy\base.py", line 20, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

Try to remove the using in the save:
def create_user(...):
...
user.save()
def create_superuser(...):
...
user.save()
EDIT:
Just noticed your routers don't have db_for_write (you wrote db_for_read twice):
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'users_db'
return None

Set your DataBase like :-
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'users_db',
'USER': 'postgres',
'PASSWORD': 'Trial_user123',
'HOST': 'localhost',
'PORT': '5432',
}
'content_db': {
'NAME': 'django.db.backends.postgresql_psycopg2',
'NAME': 'content_II',
'USER': 'postgres',
'PASSWORD': 'Trial_user123',
'HOST': 'localhost',
'PORT': '5432',
}
}
You have to migrate both databases differently like :-
$ ./manage.py migrate --database=users_db
$ ./manage.py migrate --database=content_II
Create a superuser in different database like :-
./manage.py createsuperuser --database=users_db

Related

ProgrammingError: SELECT COUNT(*) AS "__count" FROM "product"

I am using multiple databases in Django and connected default SQLite and PostgreSQL db in the settings.py.
setting.py :
DATABASE_ROUTERS = ['routers.db_routers.AppRouter']
DATABASE_APPS_MAPPING = {'product': 'postgres',}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'postgres': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'product',
'USER': 'postgres',
'PASSWORD':'password',
'HOST':'localhost'
}
}
And also made the db_routers.py in the routers folder:
class AppRouter:
"""
A router to control all database operations on models in the
product application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read user models go to postgres.
"""
if model._meta.app_label == 'product':
return 'postgres'
return 'default'
def db_for_write(self, model, **hints):
"""
Attempts to write user models go to postgres.
"""
if model._meta.app_label == 'product':
return 'postgres'
return 'default'
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the user app is involved.
"""
if obj1._meta.app_label == 'product' or \
obj2._meta.app_label == 'product':
return True
elif 'product' not in [obj1._meta.app_label, obj2._meta.app_label]:
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth app only appears in the 'product_db'
database.
"""
if app_label == 'product':
return db == 'postgres'
return None
here, it's model.py:
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
weight = models.DecimalField(max_digits=10, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
app_label = 'product'
def __str__(self):
return self.name
I have successfully made the tables by running python3 manage.py makemigrations but when I try to migrate using python3 manage.py migrate --database=postgres, I am getting this error: ProgrammingError: relation "product" does not exist. SELECT COUNT(*) AS "__count" FROM "product"And the table is also not present in PgAdmin.
migration.py of product:
# Generated by Django 3.2.5 on 2021-07-16 13:25
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Product',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('price', models.DecimalField(decimal_places=2, max_digits=10)),
('weight', models.DecimalField(decimal_places=2, max_digits=10)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
],
options={
'db_table': 'product',
'managed': False,
},
),
]
Error:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/api/product/
Django Version: 3.2.5
Python Version: 3.8.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'post',
'product']
Installed 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']
Traceback (most recent call last):
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
The above exception (relation "product" does not exist
LINE 1: ...product"."created_at", "product"."updated_at" FROM "product"
^
) was the direct cause of the following exception:
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/generics.py", line 239, in get
return self.list(request, *args, **kwargs)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/mixins.py", line 46, in list
return Response(serializer.data)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 745, in data
ret = super().data
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 246, in data
self._data = self.to_representation(self.instance)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 663, in to_representation
return [
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/models/query.py", line 280, in __iter__
self._fetch_all()
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/models/query.py", line 1324, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/models/query.py", line 51, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
cursor.execute(sql, params)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /api/product/
Exception Value: relation "product" does not exist
LINE 1: ...product"."created_at", "product"."updated_at" FROM "product"
^
After running makemigrations and then migrate,
I run my server and when I hit the endpoint- 'api/product/' I got this error - relation "product" does not exist LINE 1: ...product"."created_at", "product"."updated_at" FROM "product"
Looking at your migration file you have 'managed': False,, what does this mean? It means that you don't want the migration system to manage this model. This means that running migrate will not cause the corresponding tables to be created in the database. Don't edit migration files unless you understand what you are doing!
Remove whatever changes you might have made to the migration file, especially that 'managed': False, and then run:
python manage.py migrate product zero --fake
This will cause Django to mark all migrations for the app "product" as unapplied. This is fine since it seems like this is your initial migration, otherwise you might have wanted to change zero to some migration name. Next run:
python manage.py migrate

Django abstract user model not migrating

So I am working on implementing a custom user model, and to do so i followed tutorial 'this tutorial.
These are the models:
class UserManager(BaseUserManager):
"""Define a model manager for User model with no username field."""
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
"""Create and save a User with the given email and password."""
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
"""Create and save a regular User with the given email and password."""
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
class User(AbstractUser):
"""User model."""
username = None
email = EmailField(_('email address'), unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
I am now trying to run python3 manage.py migrate and I get this error:
[wtreston] ~/gdrive/mysite2/ $ python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, reviews, sessions, users
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 364, 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 356, 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 283, 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 330, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 164, in handle
pre_migrate_apps = pre_migrate_state.apps
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/db/migrations/state.py", line 218, in apps
return StateApps(self.real_apps, self.models)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/state.py", line 295, in __init__
raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'.
The field reviews.Answer.student was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'.
The field reviews.ReviewBlock.teacher was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'.
The field users.Student.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'.
The field users.Teacher.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'.
In my settings.py I have this line AUTH_USER_MODEL = 'users.User'
. and when I reference the AUTH_USER_MODEL with ForeignKeys, I first have from mysite import settings and then have ForeignKey(settings.AUTH_USER_MODEL)
Any help would be helpful! Thanks
You have inherited AbstractUser for your custom user model and you are inheriting BaseUserManager for UserManager
modify your manager like below by inheriting UserManager:
class MyUserManager(UserManager):
def create_user(self, email, password=None, **kwargs):
# override this method
def create_superuser(self, email, password, **kwargs):
# override this method
clean up migrations and database first and then apply migration with this changes.

Django: how to use two different databases with Wagtail CMS

Inside a Django project, I have one app, otherapp, which hits a Postgres database on a remote server that contains scraped data. I have a second app, content, which hits a different Postgres database on the same remote server, and contains pages I'd like to have served through the Wagtail CMS.
I installed Wagtail locally using these instructions (I did not use the Wagtail installer). I got it working locally. Then, I did a pg_dump of the local database and did psql db2 < db2dumpfile.sql on the remote database server.
Each of the apps works fine locally in isolation, but I can't get them to work together. I thought I could use a database router to specify which database I want used to retrieve different types of data.
But, when I put the database router into the settings file, it starts to fail. How can I fix this? Do I need to declare wagtailcore somewhere else in the project?
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db1',
'USER': DB_USERNAME,
'PASSWORD': DB_PASSWORD,
'HOST': HOST,
'PORT': PORT
},
'CMS': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db2',
'USER': DB_USERNAME,
'PASSWORD': DB_PASSWORD,
'HOST': HOST,
'PORT': PORT
}
}
DATABASE_ROUTERS = [ 'projectname.routers.FindRouter',]
routers.py:
import os
from django.conf import settings
import socket
class FindRouter(object):
def db_for_read(self, model, **hints) :
if model._meta.app_label == 'content' :
return 'CMS'
return None
def db_for_read(self, model, **hints):
if model._meta.app_label == 'content' :
return 'CMS'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'content' :
return 'CMS'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'content' or obj2._meta.app_label == 'content':
return True
return None
def allow_migrate(self, db, app_label, model=None, **hints):
if app_label == 'content' :
return db == 'CMS'
return None
This is the error I am getting when I do runserver:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/cms/
Django Version: 1.9
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'wagtail.wagtailforms',
'wagtail.wagtailredirects',
'wagtail.wagtailembeds',
'wagtail.wagtailsites',
'wagtail.wagtailusers',
'wagtail.wagtailsnippets',
'wagtail.wagtaildocs',
'wagtail.wagtailimages',
'wagtail.wagtailsearch',
'wagtail.wagtailadmin',
'wagtail.wagtailcore',
'modelcluster',
'compressor',
'taggit',
'otherapp',
'content']
Installed 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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'wagtail.wagtailcore.middleware.SiteMiddleware',
'wagtail.wagtailredirects.middleware.RedirectMiddleware']
Traceback:
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
123. response = middleware_method(request)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/wagtail/wagtailcore/middleware.py" in process_request
11. request.site = Site.find_for_request(request)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/wagtail/wagtailcore/models.py" in find_for_request
122. return Site.objects.get(hostname=hostname) # Site.DoesNotExist here goes to the final except clause
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
122. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/query.py" in get
381. num = len(clone)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/query.py" in __len__
240. self._fetch_all()
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
1074. self._result_cache = list(self.iterator())
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
52. results = compiler.execute_sql()
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
852. cursor.execute(sql, params)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/utils.py" in __exit__
95. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /cms/
Exception Value: relation "wagtailcore_site" does not exist
LINE 1: ...ge_id", "wagtailcore_site"."is_default_site" FROM "wagtailco...
^
I gave up and put the Wagtail tables back into the first database, and now the two parts of the application are working together fine.

Mongo + Postgres + Django Testing Error - Multiple Databases (Django-MongoDB-Engine)

I have been trying to get my django testing working for an API I am building. I am newer to Python and have been stuck on this for a little while.
Some quick facts:
I am using 2 databases - A postgresql database for my users and a mongo database for everything else.
I am using TastyPie, Django-MongoDB-Engine, Djangotoolbox, Django-Non-rel, Pymongo and psycopg2
I have successfully connected to the databases. When I save an auth_user from the shell it saves to the sql database and the models from my app save to the mongoDB
I have to integrate this with another API so was limited to what my options were for databases and libraries.
My big problem is with testing - I can't get it to work.
When I try to run Django's tests with ./manage.py test, I keep getting this error:
pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', u'test'), ('nonce', u'blahblah'), ('key', u'blahblah')]) failed: auth fails
After extensive search, I am still not sure what it is. I know that obviously something is trying to authenticate with my MongoDB and then its not allowing it. I dont mind using this DB to test as it is a testing DB anyways.
Please help! My configuration and error messages are below.
I set up the database settings to include both databases, as so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'main_db',
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
},
'mongo': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'mongo_db',
'USER': os.environ['MONGO_USERNAME'],
'PASSWORD': os.environ['MONGO_PASSWORD'],
'HOST': os.environ['MONGO_HOSTNAME'],
'PORT': os.environ['MONGO_PORT'],
}
}
DATABASE_ROUTERS = ['myproject.database_router.AuthRouter']
As you can see, I created my own custom router:
sql_databases = ['admin', 'auth', 'contenttypes', 'tastypie'
'sessions', 'messages', 'staticfiles']
class AuthRouter(object):
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
if model._meta.app_label in sql_databases:
return 'default'
return 'mongo'
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
if model._meta.app_label in sql_databases:
return 'default'
return 'mongo'
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations only if model does includes auth app.
"""
if obj1._meta.app_label in sql_databases or \
obj2._meta.app_label in sql_databases:
return True
return False
def allow_migrate(self, db, model):
"""
Make sure the auth app only appears in the 'auth_db'
database.
"""
if db == 'default':
return model._meta.app_label in sql_databases
elif model._meta.app_label in sql_databases:
return False
return False
The full trace is here:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
super(Command, self).run_from_argv(argv)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
super(Command, self).execute(*args, **options)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
failures = test_runner.run_tests(test_labels)
File "myproject/djenv/lib/python2.7/site-packages/django/test/runner.py", line 145, in run_tests
old_config = self.setup_databases()
File "myproject/djenv/lib/python2.7/site-packages/django/test/runner.py", line 107, in setup_databases
return setup_databases(self.verbosity, self.interactive, **kwargs)
File "myproject/djenv/lib/python2.7/site-packages/django/test/runner.py", line 279, in setup_databases
verbosity, autoclobber=not interactive)
File "myproject/djenv/lib/python2.7/site-packages/django_mongodb_engine/creation.py", line 196, in create_test_db
self.connection._reconnect()
File "myproject/djenv/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 279, in _reconnect
self._connect()
File "myproject/djenv/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 268, in _connect
if not self.database.authenticate(user, password):
File "myproject/djenv/lib/python2.7/site-packages/pymongo/database.py", line 891, in authenticate
self.connection._cache_credentials(self.name, credentials)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/mongo_client.py", line 459, in _cache_credentials
auth.authenticate(credentials, sock_info, self.__simple_command)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/auth.py", line 243, in authenticate
auth_func(credentials[1:], sock_info, cmd_func)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/auth.py", line 222, in _authenticate_mongo_cr
cmd_func(sock_info, source, query)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/mongo_client.py", line 690, in __simple_command
helpers._check_command_response(response, None, msg)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/helpers.py", line 178, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', u'test'), ('nonce', u'blahblah'), ('key', u'blahblah')]) failed: auth fails
Any help is appreciated!
Wanted to give a update on how I fixed this - I thought it was a read/write issue (with the auth fails issue), but it was not.
If you see this error, its an authentication issue - Either your username, password, database name or collection name is incorrect.
pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', u'test'), ('nonce',
u'blahblah'), ('key', u'blahblah')]) failed: auth fails
Django when setting up a test database adds 'test_' to the beginning of the database name. So my main database, called maindb is then created as 'test_maindb'.
You can see info regarding this at: Test Databases and Django
Ensure that you can write to a separate database that is named as your database name with 'test_' appended to the name.
Also - an alternative solution is to define test database setting in your database settings. This can be done by appending 'TEST_' to any of the database attributes. An example inlcudes:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'maindb',
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
'TEST_NAME': 'my_test_sql',
'TEST_USER': 'test_sql_user',
'TEST_PASSWORD': 'password'
},
'mongo': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'mongodb',
'USER': os.environ['MONGO_USERNAME'],
'PASSWORD': os.environ['MONGO_PASSWORD'],
'HOST': os.environ['MONGO_HOSTNAME'],
'PORT': os.environ['MONGO_PORT'],
'TEST_NAME': 'my_test_mongodb',
'TEST_USER': 'test_mongo_user',
'TEST_PASSWORD': 'password'
}
}
Hope it helps! And please give feedback if you see anything additional

Issue with createsuperuser when implementing custom user model

I am trying to implement my own custom user model in Django 1.6 but I am getting this error.
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 141, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() missing 1 required positional argument: 'email'
Here is my UserManager
class UserManager(BaseUserManager):
def _create_user(self, username, email, password, is_staff, is_superuser, **extra_fields):
now = timezone.now()
email = self.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=is_staff, is_active=False,
is_superuser=is_superuser, last_login=now,
date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, username, email=None, password=None, **extra_fields):
return self._create_user(username, email, password, False, False,
**extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
user=self._create_user(username, email, password, True, True,
**extra_fields)
user.is_active=True
user.save(using=self._db)
return user
It seems like this would be fairly straight forward but I can't seem to figure out why I am getting this error in the first place because I do have email specified in my create_superuser function. I have looked through several tutorials online and can't see how this is implemented differently. Can someone explain what I am doing wrong?
Looking at the code for the management commands, it only prompts for fields in the user model's REQUIRED_FIELDS attribute (as well as username). That attribute contains email by default in AbstractBaseUser, but if you have overridden it - or not inherited from that model in the first place (which you should be doing) - then email will not be prompted, and not passed to the create_superuser method.
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField('username', max_length=150, unique=True)
email = models.EmailField('email address', unique=True, max_length = 255)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
For me above code simply solved problems.
Solution :
Check the spelling of your REQUIRED_FIELDS

Categories