I have the following situation. Project files was stored in ~/django-apps/app-old/app. For some reasons I move files to ~/django-apps/app . Application have some images, stored with use of ImageField. In database images have paths like so:
~/django-apps/app-old/app/media/images/blabla.jpeg
So, I need to fix this paths in database to look like this:
~django-apps/app/media/images/blabla.jpeg
I try to write management command for to do this:
from django.core.management.base import BaseCommand, CommandError from books.models import Book import string class Command(BaseCommand): help = '' def handle(self, *args, **options): books = Book.objects.all() total = len(books) curr = 1 for book in books: print "%d/%d" % (curr, total) if book.cover_url != "": book.cover_url.path = string.replace(book.cover_url.path, "app-old/", "") book.save() curr+=1 By using this command I get following error:
Traceback (most recent call last): File "manage.py", line 11, in <module> execute_manager(settings) File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 438, in execute_manager utility.execute() File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 379, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 191, in run_from_argv self.execute(*args, **options.__dict__) File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 220, in execute output = self.handle(*args, **options) File "/var/www/dizpers/data/django-apps/app/books/management/commands/fix-covers-path.py", line 23, in handle book.cover_url.path = string.replace(book.cover_url.path, "bparser-old/", "") File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/files.py", line 65, in _get_path return self.storage.path(self.name) File "/usr/local/lib/python2.6/dist-packages/django/core/files/storage.py", line 234, in path raise SuspiciousOperation("Attempted access to '%s' denied." % name) django.core.exceptions.SuspiciousOperation: Attempted access to '/var/www/dizpers/data/django-apps/app-old/app/media/covers/a3d9545d3a17bb68a91749019c95357d.jpeg' denied. Why I get this error message? How I can fix image's path?
UPD1
My model contain ImageField like this:
cover_url = models.ImageField(upload_to=os.path.join(MEDIA_ROOT, "covers"), null=True, default=None) UPD2