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

Unable to retrieve in models.py #51

Closed
JetUni opened this issue Jul 19, 2016 · 8 comments
Closed

Unable to retrieve in models.py #51

JetUni opened this issue Jul 19, 2016 · 8 comments

Comments

@JetUni
Copy link
Contributor

JetUni commented Jul 19, 2016

Following the section below on the quickstart page, I'm unable to retrieve a global preference in my models.py. The error I get is this:
AttributeError: 'NoneType' object has no attribute 'objects'

https://django-dynamic-preferences.readthedocs.io/en/stable/quickstart.html#retrieve-and-update-preferences

I also tried a different method:
models.py

from dynamic_preferences.models import GlobalPreferenceModel

global_preferences = GlobalPreferenceModel.registry.manager()

full_name = global_preferences['Names__full_name']

And I get this error:
dynamic_preferences.exceptions.NotFoundInRegistry: No such preference in GlobalPreferenceRegistry with section=Names and name=full_name

When calling this in my views.py it works just fine

full_name = global_preferences['Names__full_name']

So why can't I get it to work in models.py?

@agateblue
Copy link
Owner

Can you please provide a full stack trace of the error, and also the content of your models.py ?

I suspect you are calling full_name = global_preferences['Names__full_name'] directly in the root of models.py meaning it is called before models are even initialized by django.

@JetUni
Copy link
Contributor Author

JetUni commented Jul 26, 2016

I'll include just the relevant lines of code from models.py

Method 1

models.py

from dynamic_preferences.registries import global_preferences_registry

global_preferences = global_preferences_registry.manager()

'company_name_full': global_preferences['Names__full_name'],
'company_name_short': global_preferences['Names__short_name'],

Stack Trace

Unhandled exception in thread started by <function wrapper at 0x7f513d429f50>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 107, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 252, in raise_last_exception
    six.reraise(*_exception)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/media/DataDisk/Sites/Jarr/dev/homestead/bidding/models.py", line 35, in <module>
    global_preferences = global_preferences_registry.manager()
  File "/usr/local/lib/python2.7/dist-packages/dynamic_preferences/registries.py", line 136, in manager
    return PreferencesManager(registry=self, model=self.preference_model, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/dynamic_preferences/managers.py", line 16, in __init__
    self.queryset = self.model.objects.all()
AttributeError: 'NoneType' object has no attribute 'objects'

Method 2

models.py

from dynamic_preferences.models import GlobalPreferenceModel

global_preferences = GlobalPreferenceModel.registry.manager()

'company_name_full': global_preferences['Names__full_name'],
'company_name_short': global_preferences['Names__short_name'],

Stack Trace

Unhandled exception in thread started by <function wrapper at 0x7f362bf45050>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 107, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 252, in raise_last_exception
    six.reraise(*_exception)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/media/DataDisk/Sites/Jarr/dev/homestead/inventory/models.py", line 36, in <module>
    """ % global_preferences['Names__full_name']
  File "/usr/local/lib/python2.7/dist-packages/dynamic_preferences/managers.py", line 27, in __getitem__
    return self.get(key)
  File "/usr/local/lib/python2.7/dist-packages/dynamic_preferences/managers.py", line 94, in get
    return db_pref.value
  File "/usr/local/lib/python2.7/dist-packages/dynamic_preferences/models.py", line 56, in get_value
    return self.preference.serializer.deserialize(self.raw_value)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 59, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python2.7/dist-packages/dynamic_preferences/models.py", line 36, in preference
    return self.registry.get(section=self.section, name=self.name)
  File "/usr/local/lib/python2.7/dist-packages/dynamic_preferences/registries.py", line 123, in get
    self.__class__.__name__, section, name))
dynamic_preferences.exceptions.NotFoundInRegistry: No such preference in GlobalPreferenceRegistry with section=Names and name=full_name

@agateblue
Copy link
Owner

agateblue commented Jul 26, 2016

That's what I thought. What you hare doing here is accessing preferences directly in the models.py (outside a class or a function). However, preferences are loaded after models (more exactly, when Apps.ready() is called).

As a result, the preference you are accessing is not loaded in the preferences registry yet.

It's not a dynamic_preferences, it's just the way django works, by the way: you should never try to query the database directly when a models.py file is imported. Because you cannot use models if they are not configured ;)

If you want to access these preferences, do it in the view, in the template, or even in a model method, anywhere after models have been fully loaded. But not at the root of the file.

@JetUni
Copy link
Contributor Author

JetUni commented Jul 26, 2016

What do you mean by "in a model method"? Sorry if I confused you. It's more like what I have below. That's not the root of the file is it?

models.py

class EmailMessage(models.Model):
  def send(self, request, etc):
    params = {
      'company_name_full': global_preferences['Names__full_name'],
      'company_name_short': global_preferences['Names__short_name'],
    }

@agateblue
Copy link
Owner

In the code you copy pasted previously, everything was at the root of your model file.

Anyway, after looking at it more thoroughly, it seems that's the manager instanciation that cause everything to crash. What happens if you move global_preferences = global_preferences_registry.manager() in your send method ?

@JetUni
Copy link
Contributor Author

JetUni commented Jul 27, 2016

Yes, that worked! Although not in another apps models.py file as there's no class or anything to put it in, so I just left it out. Some examples in the code would be nice for this. Do you care if I make something basic?

@agateblue
Copy link
Owner

In fact, you can place it anywhere, as long it is imported / executed after models are configured.

Yes, feel free to submit a pull reuest with documentation improvement, it would be useful :)

@JetUni JetUni mentioned this issue Jul 29, 2016
@JetUni
Copy link
Contributor Author

JetUni commented Jul 29, 2016

Alright, there you go!

@JetUni JetUni mentioned this issue Aug 1, 2016
agateblue pushed a commit that referenced this issue Aug 1, 2016
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

2 participants