1

I'm trying populate model from fixtures after starting Django.

I found out, that custom methods can be called in AppConfig's ready() method.

So, when I'm trying to do next:

class ApplicationConfig(AppConfig):
    name = 'application'

    def ready(self):
        from django.core.management import call_command
        call_command('loaddata', 'application/fixtures/some_info.json')

I got error message:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/app/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/app/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/app/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/app/venv/lib/python3.6/site-packages/django/apps/registry.py", line 83, in populate
    raise RuntimeError("populate() isn't reentrant")
RuntimeError: populate() isn\'t reentrant

Is there any other ways to prepopulate model on start? Or, how to fix my problem?

catscoolzhyk
  • 675
  • 10
  • 29
  • I would not, because the only case this would be useful is when running tests, and that's already implemented there. Is your data store so ephemeral that you have this concern? – Jason Dec 26 '19 at 14:33

1 Answers1

0

Use migrations file..

Within that function, just do the regular Mymodel.objects.create(yadayadayada...)

Make sure you create an EMPTY migrations file first because you need to create the table in the db first.

Yoomama
  • 133
  • 8