6

I'm running on Django 1.7 and when I run python manage.py migrate I get the following error

      File "/home/ymorin007/workspace/sites/jantiyes.com/src/deeds/migrations/0006_auto_20141204_1631.py", line 9, in <module>
    class Migration(migrations.Migration):
  File "/home/ymorin007/workspace/sites/jantiyes.com/src/deeds/migrations/0006_auto_20141204_1631.py", line 19, in Migration
    field=models.ImageField(storage=django.core.files.storage.FileSystemStorage(location=bPath('/home/ymorin007/workspace/sites/jantiyes.com/src/media')), max_length=255, null=True, upload_to=deeds.models.picture_name, blank=True),
NameError: name 'bPath' is not defined

This is my deeds/models.py

from jantiyes.settings.base import MEDIA_ROOT

upload_storage = FileSystemStorage(location=MEDIA_ROOT)

def picture_name(self, filename):

    ext = filename.split('.')[-1]
    deedname = re.sub('[ ]', '-', self.text.lower())
    filename = "DEED-%s-%s.%s" % (self.id, deedname, ext)

    url = "%s" % filename

    return url


class Deed(TimeStampedModel):

    picture = models.ImageField(upload_to=picture_name, null=True, blank=True, storage=upload_storage, max_length=255)
    text = models.CharField(max_length=500)
    when = models.DateField(unique=True)

My Media declaration:

BASE_DIR = Path(__file__).ancestor(3)
MEDIA_ROOT = BASE_DIR.child("media")
Yannick
  • 3,553
  • 9
  • 43
  • 60

2 Answers2

7

It's hard to tell without the exact definition of jantiyes.settings.base.MEDIA_ROOT but I guess it's an instance of a class (bPath) that is not deconstructible and that is a subclass of unicode. Hence the migration writer assumes it doesn't need any imports and simply repr the value which turns out to be bPath('/home/ymorin007/workspace/sites/jantiyes.com/src/media').

You have two options:

  1. Make sure jantiyes.settings.base.MEDIA_ROOT is defined as string and thus correctly handled by the migration writer. e.g. MEDIA_ROOT = '/home/ymorin007/workspace/sites/jantiyes.com/src/media' in your jantiyes.settings.base module file.
  2. Make sure the bPath class is deconstructible by defining a deconstruct method returning the import path to itself.
Simon Charette
  • 5,009
  • 1
  • 25
  • 33
  • my media declaration path. BASE_DIR = Path(__file__).ancestor(3) MEDIA_ROOT = BASE_DIR.child("media") – Yannick Dec 18 '14 at 12:47
  • Again, it's hard to tell without the details about the origin of the `Path` class but I'd guess `MEDIA_ROOT = str(BASE_DIR.child("media"))` should do? – Simon Charette Dec 18 '14 at 16:36
  • str(BASE_DIR.child("media")) fix everything...Bravo – Yannick Dec 18 '14 at 17:41
  • from the error it doesn't sound like anything to do with deconstructibility... it looks more like migration tries to import a helper function `bPath` which perhaps no longer exists in the project – Anentropic Dec 20 '14 at 04:12
  • @Anentropic the `0006_auto_20141204_1631.py` file was automatically generated by Django's migration framework. I pretty sure my answer is right but no one can tell for sure unless @Yannick provides the name of the third-party package `Path` is imported from. – Simon Charette Dec 20 '14 at 05:51
  • it doesn't matter that the migration was auto-generated, the error message and traceback are pretty clear `bPath` is a function which hasn't been imported/doesn't exist `NameError: name 'bPath' is not defined` – Anentropic Dec 20 '14 at 10:10
  • `name 'bPath' is not defined` because it was not automatically imported when the migration was generated. Did you take the time to learn how the migration framework deconstruct an application state to generate those files? – Simon Charette Dec 20 '14 at 16:21
  • I have the same issue using fs_private_folder = FileSystemStorage(location=settings.PRIVATE_FOLDER_ROOT) and models.FileField('file', storage=fs_private_folder, upload_to=get_upload_path_message_file). Could you tell me what it is wrong? – Daviddd Feb 27 '15 at 14:46
  • What about `fs_private_folder = FileSystemStorage(location=str(settings.PRIVATE_FOLDER_ROOT))` ? – Simon Charette Feb 27 '15 at 17:53
1

Where is MEDIA_ROOT defined? I'm assuming it's defined in your settings file, in which case you likely need

from django.conf import settings

upload_storage = FileSystemStorage(location=settings.MEDIA_ROOT)
Jared
  • 4,567
  • 2
  • 26
  • 30
  • Yes I have from jantiyes.settings.base import MEDIA_ROOT – Yannick Dec 05 '14 at 13:53
  • My guess then is that it doesn't like `upload_storage` is defined outside of an `__init__` function. Code inside of an `__init__` function only gets called at instantiation, but code outside of it gets called whenever you run `migrate` or `runserver`. – Jared Dec 05 '14 at 18:08