Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LookupError: App 'dynamic_preferences' doesn't have a 'userpreferencemodel' model. #141

Closed
gonvaled opened this issue Apr 23, 2018 · 6 comments

Comments

@gonvaled
Copy link

gonvaled commented Apr 23, 2018

Starting with a blank database, I am having problems running migrations:

    $ python manage.py migrate

    Applying dynamic_preferences.0001_initial...Traceback (most recent call last):
      File "/usr/local/lib/python3.5/site-packages/django/apps/config.py", line 163, in get_model
        return self.models[model_name.lower()]
    KeyError: 'userpreferencemodel'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "manage.py", line 22, in <module>
        execute_from_command_line(sys.argv)
      File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
        utility.execute()
      File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
        output = self.handle(*args, **options)
      File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
        fake_initial=fake_initial,
      File "/usr/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 "/usr/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 "/usr/local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
        state = migration.apply(state, schema_editor)
      File "/usr/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 "/usr/local/lib/python3.5/site-packages/django/db/migrations/operations/models.py", line 95, in database_forwards
        if self.allow_migrate_model(schema_editor.connection.alias, model):
      File "/usr/local/lib/python3.5/site-packages/django/db/migrations/operations/base.py", line 115, in allow_migrate_model
        return router.allow_migrate_model(connection_alias, model)
      File "/usr/local/lib/python3.5/site-packages/django/db/utils.py", line 311, in allow_migrate_model
        model=model,
      File "/usr/local/lib/python3.5/site-packages/django/db/utils.py", line 300, in allow_migrate
        allow = method(db, app_label, **hints)
      File "/usr/local/lib/python3.5/site-packages/rest_models/router.py", line 105, in allow_migrate
        model = apps.get_model(app_label, model_name)
      File "/usr/local/lib/python3.5/site-packages/django/apps/registry.py", line 195, in get_model
        return self.get_app_config(app_label).get_model(model_name.lower())
      File "/usr/local/lib/python3.5/site-packages/django/apps/config.py", line 166, in get_model
        "App '%s' doesn't have a '%s' model." % (self.label, model_name))
    LookupError: App 'dynamic_preferences' doesn't have a 'userpreferencemodel' model.

Doing showmigrations I see:

    $ python manage.py showmigrations

    ...
     [X] 0007_alter_validators_add_error_messages
     [X] 0008_alter_user_username_max_length
    captcha
     [X] 0001_initial
    contenttypes
     [X] 0001_initial
     [X] 0002_remove_content_type_name
    database
     [X] 0001_initial
    dynamic_preferences
     [ ] 0001_initial
     [ ] 0002_auto_20150712_0332
     [ ] 0003_auto_20151223_1407
     [ ] 0004_move_user_model
    dynamic_preferences_users
     [ ] 0001_initial
    ...

What could be the problem?

@gonvaled gonvaled changed the title Problems in migrations for dynamic_preferences LookupError: App 'dynamic_preferences' doesn't have a 'userpreferencemodel' model. Apr 23, 2018
@gonvaled
Copy link
Author

In development, removing all migrations for dynamic_preferences and recreating them works:

  ...
  Applying dynamic_preferences.0001_initial... OK
  Applying dynamic_preferences_users.0001_initial... OK
  ...

But this is not an acceptable workaround because I can not run this in production, where I can not remove the migrations of third-party applications.

@marcofvera
Copy link

marcofvera commented Jun 13, 2018

I'm having a similar issue with a blank database but in my case it seems something related to global_preference_form_builder() function.

I can't do anything in my django environment. Any command I enter I receive this error (including python manage.py migrate):

Traceback (most recent call last):
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: dynamic_preferences_globalpreferencemodel

@agateblue
Copy link
Owner

Hi @gonvaled and @marcofvera.

I'm sorry for remaining silent for such a long time. I have little times these days.

I think the issue you're facing is caused by #140. I'll take a few hours today to start a new project, reproduce the issue and find a possible workaround.

@agateblue
Copy link
Owner

@marcofvera I cannot reproduce your issue, so I'd like to know more about your project. Is there any minimum code that reproduces the issue you could share ? Or at least a full stack trace ?

agateblue pushed a commit that referenced this issue Jun 17, 2018
See #141 and #140, migration cleanup and django2/python3.6 compat
@marcofvera
Copy link

@EliotBerriot I'm sorry for the delay.. I just saw your comment now. I've tried to use the version 1.6 but still not working on my environment. Here is the full stack trace:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/core/management/base.py", line 327, in execute
    self.check()
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/urls/resolvers.py", line 398, 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/marco/git/funnydation/funnydation/funnydation/urls.py", line 7, in <module>
    from funnydation.views import HomeView, LoginView, LogoutView, PreferencesView
  File "/Users/marco/git/funnydation/funnydation/funnydation/views.py", line 42, in <module>
    class PreferencesView(FormMixin, TemplateView):
  File "/Users/marco/git/funnydation/funnydation/funnydation/views.py", line 43, in PreferencesView
    form_class = global_preference_form_builder(section='general')
  File "/Users/marco/funnydation/lib/python3.6/site-packages/dynamic_preferences/forms.py", line 126, in global_preference_form_builder
    return preference_form_builder(GlobalPreferenceForm, preferences, **kwargs)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/dynamic_preferences/forms.py", line 109, in preference_form_builder
    section=preference.section.name, name=preference.name)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/dynamic_preferences/managers.py", line 146, in get_db_pref
    pref = self.queryset.get(section=section, name=name)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/models/query.py", line 374, in get
    num = len(clone)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/models/query.py", line 232, in __len__
    self._fetch_all()
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/models/query.py", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 899, in execute_sql
    raise original_exception
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 889, in execute_sql
    cursor.execute(sql, params)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/marco/funnydation/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: dynamic_preferences_globalpreferencemodel

Here is part of the code where I'm using the global_preference_form_builder() function:

class PreferencesView(FormMixin, TemplateView):
    form_class = global_preference_form_builder(section='general')
    success_url = reverse_lazy('preferences')
    template_name = 'preferences.html'

@agateblue
Copy link
Owner

@marcofvera this happens because you are creating the form at module level, which requires database access.

Return the form class from the get_form_class method instead :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants