Giter Site home page Giter Site logo

Comments (8)

agateblue avatar agateblue commented on August 20, 2024

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.

JetUni avatar JetUni commented on August 20, 2024

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.

agateblue avatar agateblue commented on August 20, 2024

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.

JetUni avatar JetUni commented on August 20, 2024

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.

agateblue avatar agateblue commented on August 20, 2024

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.

JetUni avatar JetUni commented on August 20, 2024

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.

agateblue avatar agateblue commented on August 20, 2024

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.

JetUni avatar JetUni commented on August 20, 2024

Alright, there you go!

from django-dynamic-preferences.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.