question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to retrieve in models.py

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
agatebluecommented, 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.

0reactions
JetUnicommented, Jul 29, 2016

Alright, there you go!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found