-
Notifications
You must be signed in to change notification settings - Fork 86
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
Comments
Can you please provide a full stack trace of the error, and also the content of your I suspect you are calling |
I'll include just the relevant lines of code from Method 1
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
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 |
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 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. |
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?
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'],
} |
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 |
Yes, that worked! Although not in another apps |
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 :) |
Alright, there you go! |
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
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 fineSo why can't I get it to work in
models.py
?The text was updated successfully, but these errors were encountered: