Search code examples
pythondjangofixturesfactory-boy

Load data that has models with ImageField in django


Any idea how to load data that has models with ImageField in django? I cannot use fixtures for this as there’s no way to serialize images into the fixtures files.

I’m trying to use factory-boy which works very well for tests but I cannot see how to run it as a standalone script because there’s no django management command for it.

Is python manage.py shell < myscript.py a good approach to handle this or there’s a better way?

Note that I’m using a bucket for file storage with salted filenames, and I have signals that do stuff like indexing into Elasticsearch which I need to keep. and fixtures don’t do this.


Solution

  • Well, the question is actually about having django env for your script, I've misunderstood the question.

    1. You may use shell < script.py, viable one-timer
    2. Make your custom management command. Ok if this is something regular, main benefit is "maintainability" — intention will be more clear for other people, plus manage.py integrations with IDE may be good
    3. Your script may setup django env itself — same as manage.py shell does, there's not much of black magic, something like

      import os, django
      os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
      django.setup()