berinhard/model_mommy

The SECRET_KEY setting must not be empty

epicserve opened this issue · 8 comments

After upgrading to 1.3.1 from 1.3.0 I'm getting the error:

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Looking at the traceback, this exception starts from my settings file where I have the following for generating a string for a custom field.

from model_mommy.generators import gen_string

MOMMY_CUSTOM_FIELDS_GEN = {
    'localflavor.us.models.PhoneNumberField': gen_string,
}

The SECRET_KEY is defined in that same setting file, way before it gets to this part of the code. I'm not sure how to solve this issue. I tried switching my code to the following after looking at the documentation, but it didn't work either.

from model_mommy import mommy
mommy.generators.add('localflavor.us.models.PhoneNumberField', 'model_mommy.generators.gen_string')

I should also note that I'm using Django 1.10.3 and Python 3.5.1.

Seeing this issue as well -- we have the following in our test_settings.py file:

from ipaddress import ip_interface
from model_mommy.generators import gen_ipv4
from settings import *

# model mommy generators for custom fields
def gen_inet_address_field():
    return ip_interface(gen_ipv4())

MOMMY_CUSTOM_FIELDS_GEN = {
    'peering.apps.srvmgr.fields.InetAddressField': gen_inet_address_field,
}

SECRET_KEY is defined in settings.py and set based on an envvar. from model_mommy.generators import gen_ipv4 seems to cause Django to complain about SECRET_KEY not being set. However, changing the import order also doesn't fix things -- just a different complaint from Django:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 302, in execute
    settings.INSTALLED_APPS
  File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 99, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/src/app/peering/test_settings.py", line 10, in <module>
    from model_mommy.generators import gen_ipv4
  File "/usr/local/lib/python2.7/site-packages/model_mommy/generators.py", line 1, in <module>
    from django.contrib.contenttypes.models import ContentType
  File "/usr/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 159, in <module>
    class ContentType(models.Model):
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 94, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
    self.check_apps_ready()
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Using mommy.generators.add should work, if you do it outside of the settings file.
If you are using py.test I would recommend doing so in a conftest.py file. If you are using UnitTest you could ether register the generator in the mommy_recipes.py file or the models.py file where you use a target field.

@codingjoe, is this in the documentation somewhere and I just missed it? Do you have an example of what registering "the generator in the models.py file where you use a target field", would look like?

@epicserve good point, I don't really know. Someone could add a best practice approach.
At the end it needs to be done before you call mommy.make or mommy.prepare.
If you do it on a models.py it gets automatically loaded. If you do it in a mommy_recipes.py it happens when you load the recipe.

At the end the best place would be where you define the field. So you write your field and than you define the mommy generator. Since that isn't possible for 3rd party packages, you will need to put it somewhere in you own code.

The settings get loaded always, but I still wouldn't do it, since i would avoid importing things into the settings file.
As an alternative you can do it with late importing.

mommy.generators.add('path.to.field', 'path.to.generator')

This will also work in settings.py and is documented that way.
http://model-mommy.readthedocs.io/en/latest/how_mommy_behaves.html#custom-fields

@codingjoe

I tried the following in my settings and I'm still getting the same error.

from model_mommy import generators
generators.add('localflavor.us.models.PhoneNumberField', 'mommy.generators.gen_string')

@epicserve while we sort this out you may follow @codingjoe suggestion (#299 (comment)) and define this outside of settings.py

@epicserve sorry, that I never got to respond. The whole point is to NOT do it in the settings file.
Please do it somewhere else, preferably wherever you dine your new field. If it's a third party like localflavor, I would recommend to register it in you test runner.

Hi everyone, the point that was causing the issue was that the generators module was depending upon django's imports. I've encapsulated all of those imports and not the module can be imported even if django's not configured. The fix was introduced here: 8ac3f7e.

Here's the working proof:

(model_mommy) [bernardo@macalandia ~/envs/model_mommy(development)]$ pip uninstall django
Uninstalling Django-1.11.13:
  Would remove:
    /home/bernardo/.virtualenvs/model_mommy/bin/django-admin
    /home/bernardo/.virtualenvs/model_mommy/bin/django-admin.py
    /home/bernardo/.virtualenvs/model_mommy/lib/python2.7/site-packages/Django-1.11.13.dist-info/*
    /home/bernardo/.virtualenvs/model_mommy/lib/python2.7/site-packages/django/*
Proceed (y/n)? y
  Successfully uninstalled Django-1.11.13
(model_mommy) [bernardo@macalandia ~/envs/model_mommy(development)]$ ipython
Python 2.7.14 (default, Sep 23 2017, 22:06:14) 
Type "copyright", "credits" or "license" for more information.

IPython 5.6.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from model_mommy.random_gen import gen_float

In [2]: gen_float()
Out[2]: 5225.080428205997