doableware / djongo

Django and MongoDB database connector
https://www.djongomapper.com
GNU Affero General Public License v3.0
1.89k stars 355 forks source link

Problem with rest_framework.authtoken #11

Closed wylswz closed 6 years ago

wylswz commented 7 years ago

One line description of the issue

I was migrating my django project from MySQL to mongodb. When I apply the migration, this issue occurs. It seems like it's not working well with restframework.

Python script

```python """ Django settings for learning_log project. Generated by 'django-admin startproject' using Django 1.11.2. For more information on this file, see https://docs.djangoproject.com/en/1.11/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.11/ref/settings/ """ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'hwlu6e=0(goqrt19!+fz-1zhqor%9b$mzq7^ovth!s0112%tbk' # 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', 'learning_logs', 'users', 'visual', #visualization 'social_django', #auth 'bootstrap3', 'password_reset', 'file', 'markdownx', 'django_tables2', 'portfolios', 'risk', 'clients', 'rest_framework', 'rest_framework.authtoken', 'APIs' ] REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ), 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication', ) } MIDDLEWARE_CLASSES = [ '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', 'social_django.middleware.SocialAuthExceptionMiddleware', #social ] ROOT_URLCONF = 'learning_log.urls' TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', ) TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR,'templates'),], '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', 'social_django.context_processors.backends', 'social_django.context_processors.login_redirect', 'learning_log.my_context.profile', ], }, }, ] AUTHENTICATION_BACKENDS = ( 'social_core.backends.github.GithubOAuth2', 'social_core.backends.twitter.TwitterOAuth', 'social_core.backends.facebook.FacebookOAuth2', 'social_core.backends.google.GoogleOAuth2', 'social_core.backends.google.GoogleOAuth', 'django.contrib.auth.backends.ModelBackend', ) WSGI_APPLICATION = 'learning_log.wsgi.application' # Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases '''DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'USER':'root', 'NAME': 'AOX', 'PASSWORD':'******' } }''' DATABASES = { 'default': { 'ENGINE': 'djongo', 'NAME': 'AOX', 'USER':'root', 'PASSWORD':'******' } } # Password validation # https://docs.djangoproject.com/en/1.11/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/1.11/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/1.11/howto/static-files/ STATIC_URL = '/static/' STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static_cdn") STATICFILES_DIRS = ( os.path.join('static'), ) #My settings LOGIN_URL='/users/login/' LOGIN_REDIRECT_URL = '/' SOCIAL_AUTH_GITHUB_KEY = '59a91766268c5d94cc7d' SOCIAL_AUTH_GITHUB_SECRET = 'd507e39aa527ecf840cf7920222c22be15951d3f' IL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # Host for sending e-mail. #EMAIL_HOST = 'localhost' # Port for sending e-mail. #EMAIL_PORT = 1025 SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '6707083920-ickukm3qpgu2k7718e812s7fgah289vs.apps.googleusercontent.com' SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'mhlItMxZKj1lNECGkEc3HHYk' BOOTSTRAP3 = { 'include_jquery':True, } if DEBUG: EMAIL_HOST = 'localhost' EMAIL_PORT = 1025 EMAIL_HOST_USER = '' EMAIL_HOST_PASSWORD = '' EMAIL_USE_TLS = False DEFAULT_FROM_EMAIL = 'testing@example.com' ``` #### Traceback python3 manage.py migrate /usr/local/lib/python3.5/dist-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead. from pandas.core import datetools System check identified some issues: WARNINGS: ?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_LOADERS. ?: (urls.W005) URL namespace 'social' isn't unique. You may not be able to reverse all URLs in this namespace visual.FinalPricingFile.committed: (fields.W122) 'max_length' is ignored when used with IntegerField HINT: Remove 'max_length' from field visual.priceResults.date: (fields.W122) 'max_length' is ignored when used with IntegerField HINT: Remove 'max_length' from field visual.priceResults.iv: (fields.W122) 'max_length' is ignored when used with IntegerField HINT: Remove 'max_length' from field Operations to perform: Apply all migrations: APIs, admin, auth, authtoken, contenttypes, file, learning_logs, sessions, social_django, users, visual Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying APIs.0001_initial... OK Applying APIs.0002_auto_20170829_0607... 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 authtoken.0001_initial... OK Applying authtoken.0002_auto_20160226_1747...Traceback (most recent call last): File "manage.py", line 22, in execute_from_command_line(sys.argv) File "/home/johnny/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line utility.execute() File "/home/johnny/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/johnny/.local/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv self.execute(*args, **cmd_options) File "/home/johnny/.local/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute output = self.handle(*args, **options) File "/home/johnny/.local/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle fake_initial=fake_initial, File "/home/johnny/.local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) File "/home/johnny/.local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) File "/home/johnny/.local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration state = migration.apply(state, schema_editor) File "/home/johnny/.local/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "/home/johnny/.local/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards schema_editor.alter_field(from_model, from_field, to_field) File "/home/johnny/.local/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 515, in alter_field old_db_params, new_db_params, strict) File "/home/johnny/.local/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 524, in _alter_field fk_names = self._constraint_names(model, [old_field.column], foreign_key=True) File "/home/johnny/.local/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 989, in _constraint_names constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table) File "/home/johnny/.local/lib/python3.5/site-packages/django/db/backends/base/introspection.py", line 180, in get_constraints raise NotImplementedError('subclasses of BaseDatabaseIntrospection may require a get_constraints() method') NotImplementedError: subclasses of BaseDatabaseIntrospection may require a get_constraints() method
nesdis commented 7 years ago

Hi this looks like a genuine djongo issue. Django connector expects several interfaces to be implemented and not all up yet! these will be implemented at some time.

ravinarayansingh commented 6 years ago

@nesdis any suggestion how can we resolve this issue and please explain to me, I will work on this issue and contribute to this project. My problem is i want to write unit tests for our DRF API. But Django will create database so we are getting

NotImplementedError: subclasses of BaseDatabaseIntrospection may require a get_constraints() method

I am stuck :(

nesdis commented 6 years ago

In file introspection.py in djongo, you must implement the get_constraints function. The function takes in the collection name and returns a dict of properties.

You need to use pymongo commands to get the constraints for different fields.

Some constraints like primary key, have no meaning in mongoDB, You need to store the primary key field name in schema , and return this in this function.

Indexing support needs to be added to djongo, you can then return indexed fields.

Returns a dict mapping constraint names to their attributes, where attributes is a dict with keys:

ayakut commented 6 years ago

Hi guys,

Your solution looks greats!

But I have the same issue: (fifa_mongo) C:\Users\alexk\development\projects\python\fifa_mongo>manage.py migrate --settings=settings_dev Operations to perform: Apply all migrations: admin, auth, contenttypes, django_celery_results, flows, frontend, sessions, viewflow 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 django_celery_results.0001_initial... OK Applying viewflow.0001_initial... OK Applying viewflow.0002_fsmchange... OK Applying viewflow.0003_task_owner_permission_change... OK Applying viewflow.0004_extend_fields_length... OK Applying viewflow.0005_rename_flowcls... OK Applying viewflow.0004_subprocess... OK Applying viewflow.0005_merge... OK Applying viewflow.0006_merge... OK Applying viewflow.0007_owner_permission_obj... OK Applying viewflow.0006_i18n...Traceback (most recent call last): File "C:\Users\alexk\development\projects\python\fifa_mongo\manage.py", line 22, in execute_from_command_line(sys.argv) File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\core\management__init.py", line 364, in execute_from_command_line utility.execute() File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\core\management\init__.py", line 356, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv self.execute(*args, *cmd_options) File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\core\management\base.py", line 330, in execute output = self.handle(args, **options) File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle fake_initial=fake_initial, File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration state = migration.apply(state, schema_editor) File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\db\migrations\migration.py", line 129, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\db\migrations\operations\fields.py", line 216, in database_forwards schema_editor.alter_field(from_model, from_field, to_field) File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\db\backends\base\schema.py", line 515, in alter_field old_db_params, new_db_params, strict) File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\db\backends\base\schema.py", line 524, in _alter_field fk_names = self._constraint_names(model, [old_field.column], foreign_key=True) File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\db\backends\base\schema.py", line 989, in _constraint_names constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table) File "C:\Users\alexk\development\projects\python\venvs\fifa_mongo\lib\site-packages\django\db\backends\base\introspection.py", line 180, in get_constraints raise NotImplementedError('subclasses of BaseDatabaseIntrospection may require a get_constraints() method')

CONTEXT: OS: Windows 10 IDE: PyCharm 2017.3 Python: 3.6.2 Mongo cluster

SETTINGS: ` ... INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django_celery_results', 'django_extensions', 'material', 'material.frontend', 'viewflow', 'viewflow.frontend', 'fifa.flows', ] ... 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 = 'fifa.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 = 'wsgi.application' ... MIDDLEWARE.insert(0, 'debug_toolbar.middleware.DebugToolbarMiddleware')

DATABASES = { 'default': { 'ENGINE': 'djongo', 'NAME': 'fifa_mongo', 'HOST': 'dev-hybrid-mongodb-service-us', 'PORT': 27017, } } .... `

Please, could you provide me some more detailed solution? Thank you!

nesdis commented 6 years ago

Get constraints has been implemented, you may want to clone the latest version and check if it works for your project

ayakut commented 6 years ago

Hi guys,

Thank you for the update!

The "pip install djongo==" shows me the last version is 1.2.9 Collecting djongo== Could not find a version that satisfies the requirement djongo== (from versions: 1.1, 1.2, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.2.6, 1.2.7, 1.2.8, 1.2.9) No matching distribution found for djongo==

Should we ignore the "pip install" and try to work via cloning of the project?

Thank you!

nesdis commented 6 years ago

Yes clone the project..

On 06-Dec-2017 1:00 AM, "ayakut" notifications@github.com wrote:

Hi guys,

Thank you for the update!

The "pip install djongo==" shows me the last version is 1.2.9 Collecting djongo== Could not find a version that satisfies the requirement djongo== (from versions: 1.1, 1.2, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.2.6, 1.2.7, 1.2.8, 1.2.9) No matching distribution found for djongo==

Should we ignore the "pip install" and try to work via cloning of the project?

Thank you!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nesdis/djongo/issues/11#issuecomment-349414586, or mute the thread https://github.com/notifications/unsubscribe-auth/AQO4BL_o4QhDn7bNfEHYYJoaITEZ4UIUks5s9Zm-gaJpZM4Pexty .

nesdis commented 6 years ago

A new version has been updated to pip as well. get_constratins has been implemented

ayakut commented 6 years ago

Hi guys!

Thanks a lot for the update and for your supporting!