Django: __init__.py package breaks migrations

88 views Asked by At

In my app, I have file structure:

myapp/
    ...
    models.py
    helpers/
        __init__.py
        RandomFileName.py
    ...

In RandomFileName.py I have helper class that generates random file names for my models:

class RandomFileName(object):
    ...

In models I want to treat helpers/ directory as a module:

from myapp.helpers import RandomFileName

class MyImage(models.Model):
    ...
    image = models.ImageField(upload_to=RandomFileName('images/'))
    ...

Then, I run python3 manage.py makemigrations myapp Looks good.

Then, I run python3 manage.py migrate and get an error:

in Migration
  ('image', models.ImageField(upload_to=myapp.helpers.RandomFileName.RandomFileName('images/'))),
AttributeError: type object 'RandomFileName' has no attribute 'RandomFileName'

Why is the RandomFileName doubled in migrations? Where did I go wrong?

1

There are 1 answers

0
acw On

Somehow your init.py file could have imported your object. Check. If not, then simply doing myapp.helpers.RandomFileName('images/') instead (based on the exception message) will fix the issue.