Unable to retrieve in models.py
See original GitHub issueFollowing 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'
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
?
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Failed to retrieve list of Django objects with models.Manager
I have the following structure scenario in my models.py : from django.db import models class SensorManager(models.
Read more >Gurobi error : unable to retrieve attribute 'Xn' - Google Groups
I have a facility location problem and I need to ge the best two solutions so in my code I am utilizing the...
Read more >T0/T1: platform_tests/api/test_thermal.py::TestThermalApi ...
In platform_tests/api/test_thermal.py, API returns None for test_get_model and ... Unable to retrieve model and serial number #4368.
Read more >Django Models - GeeksforGeeks
Retrieving objects. To retrieve all the objects of a model, we write the following command: >>> GeeksModel.objects.all() <QuerySet ...
Read more >Models - Django documentation
Each model is a Python class that subclasses django.db.models. ... are provided to Django models and is used to retrieve the instances from...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
Alright, there you go!