Comments (8)
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.
from django-dynamic-preferences.
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
from django-dynamic-preferences.
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.
from django-dynamic-preferences.
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'],
}
from django-dynamic-preferences.
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 ?
from django-dynamic-preferences.
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?
from django-dynamic-preferences.
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 :)
from django-dynamic-preferences.
Alright, there you go!
from django-dynamic-preferences.
Related Issues (20)
- Default value is not applied back after clearing a preference HOT 3
- Support other cache than 'default' HOT 1
- "POST" API request not supported by GlobalPreferencesViewSet
- How to set a django setting dynamicly? HOT 4
- FloatPreference should be able to be initialized from both floats and ints
- Missing migration in version 1.12
- Not update affecting in Django channel HOT 1
- Divide preferences by section in the admin interface - Signals problem HOT 13
- Add support for Graphene-Django HOT 3
- PerInstancePreferenceAdmin raises MultipleObjectsReturned while saving a per instance preference
- django.core.files.uploadedfile.InMemoryUploadedFile not supported in s3transfer HOT 1
- RemovedInDjango41Warning: 'dynamic_preferences' defines default_app_config HOT 2
- ModelMultipleChoicePreference update not working HOT 2
- New release + Django 4 support? HOT 1
- Mocking global preferences when testing HOT 2
- Documentation about FilePreference is too light HOT 3
- make a release HOT 1
- Caching is not invalidated HOT 8
- Is it possible to access the preferences in annotations? HOT 1
- Tests do not run with Python 3.12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-dynamic-preferences.