I try to implement an image field that you can assign a string to and it will automatically fetch the image from that URL. Reading it afterwards, it will hold the path to the local copy. Therefore, I inherited from Django's ImageField
and its descriptor class.
import uuid
import urllib.request
from django.core.files.base import ContentFile
from django.db import models
from django.db.models.fields.files import ImageFileDescriptor, ImageFieldFile
class UrlImageFileDescriptor(ImageFileDescriptor):
def __init__(self, field):
super().__init__(field)
def __get__(self, instance, owner=None):
# Get path to local copy on the server
try:
file = super().__get__(instance)
print('Get path to local copy', file.url)
return file.url if file else None
except:
return None
def __set__(self, instance, value):
# Set external URL to fetch new image from
print('Set image from URL', value)
# Validations
if not value:
return
value = value.strip()
if len(value) < 1:
return
if value == self.__get__(instance):
return
# Fetch and store image
try:
response = urllib.request.urlopen(value)
file = response.read()
name = str(uuid.uuid4()) + '.png'
content = ContentFile(file, name)
super().__set__(instance, content)
except:
pass
class UrlImageField(models.ImageField):
descriptor_class = UrlImageFileDescriptor
When saving the model that uses this field, Django code raises an error 'str' object has no attribute '_committed'
. This is the related code from Django 1.7c1. It lives in db/models/fields/files.py. The exception occurs on the line of the if statement.
def pre_save(self, model_instance, add):
"Returns field's value just before saving."
file = super(FileField, self).pre_save(model_instance, add)
if file and not file._committed:
# Commit the file to storage prior to saving the model
file.save(file.name, file, save=False)
return file
I don't understand file
being a string here. The only thing I can think of is that the descriptor classes __get__
returns string. However, it calls the __get__
of its base class with a ContentFile
, so that should be stored in the __dict__
of the model. Can someone explain this to me? How can I find a workaround?