是否可以在django和另一个数据库中使用一个数据库进行用户身份验证以使用django-forms?我使用非关系数据结构,并对mongodb表单感到满意,但我无法使用mongodb授权我的用户。所有帖子都描述mongodb-authentification与django referer对mongoengine的弃用模块,不再支持django。所以我决定使用mysql进行用户身份验证,使用mongodb作为主数据库,但是找不到有关为用户授权定义特殊数据库的信息。
UPD: 我将mysql添加到setings:
DATABASES = {
'default': {
'ENGINE' : 'django.db.backends.dummy',
'NAME' : 'my_database'
},
'users': {
'NAME': 'my_users',
'ENGINE': 'django.db.backends.mysql',
'USER': 'django'
}
}
迁移:
python3 manage.py migrate --database=users
它返回:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
然后我尝试创建一个用户:
set DJANGO_SETTINGS_MODULE=my_site.settings
,
>>> import django
>>> django.setup()
>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/models.py", line 159, in create_user
return self._create_user(username, email, password, **extra_fields)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/models.py", line 153, in _create_user
user.save(using=self._db)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/base_user.py", line 80, in save
super(AbstractBaseUser, self).save(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 834, in save_base
with transaction.atomic(using=using, savepoint=False):
File "/usr/local/lib/python3.5/dist-packages/django/db/transaction.py", line 158, in __enter__
if not connection.get_autocommit():
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py", line 385, in get_autocommit
self.ensure_connection()
File "/usr/local/lib/python3.5/dist-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.
>>>
伙计们,请告诉我,出了什么问题?
UPD2: 添加路由器到settings.py:
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 == 'auth':
return 'users'
return 'default'
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
if model._meta.app_label == 'auth':
return 'users'
return 'default'
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the auth app is involved.
"""
if obj1._meta.app_label == 'auth' or \
obj2._meta.app_label == 'auth':
return True
return False
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth app only appears in the 'auth_db'
database.
"""
return db == 'auth_db'
DATABASE_ROUTERS = [AuthRouter]
现在我在创建用户时出现了错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/models.py", line 159, in create_user
return self._create_user(username, email, password, **extra_fields)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/models.py", line 153, in _create_user
user.save(using=self._db)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/base_user.py", line 80, in save
super(AbstractBaseUser, self).save(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 766, in save
using = using or router.db_for_write(self.__class__, instance=self)
File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 267, in _route_db
chosen_db = method(model, **hints)
TypeError: db_for_write() missing 1 required positional argument: 'model'
我的路由器出了什么问题?我从官方文档中复制了它。