0

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?

danijar
  • 32,406
  • 45
  • 166
  • 297

1 Answers1

1

The problem is that you need to return a FieldFile, so that way you can access to the properties of it, in django's source code you can find a class named FileDescriptor this is the parent of ImageFileDescriptor, if you look at the FileDescriptor class under the name you can find the doc of the class and it says:

 """
    The descriptor for the file attribute on the model instance. Returns a
    FieldFile when accessed so you can do stuff like::

        >>> from myapp.models import MyModel
        >>> instance = MyModel.objects.get(pk=1)
        >>> instance.file.size

    Assigns a file object on assignment so you can do::

        >>> with open('/tmp/hello.world', 'r') as f:
        ...     instance.file = File(f)

    """

So you need to return a FieldFile not a String just do it changing the return for this.

return None or file

UPDATE:

I figured out your problem and this code works for me:

import uuid
import requests

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(UrlImageFileDescriptor, self).__init__(field)

    def __set__(self, instance, value):
        if not value:
            return
        if isinstance(value, str):
            value = value.strip()
            if len(value) < 1:
                return
            if value == self.__get__(instance):
                return
            # Fetch and store image
            try:
                response = requests.get(value, stream=True)
                _file = ""
                for chunk in response.iter_content():
                    _file+=chunk
                headers = response.headers
                if 'content_type' in headers:
                    content_type = "." + headers['content_type'].split('/')[1]
                else:
                    content_type = "." + value.split('.')[-1]
                name = str(uuid.uuid4()) + content_type
                value = ContentFile(_file, name)
            except Exception as e:
                print e
                pass
        super(UrlImageFileDescriptor,self).__set__(instance, value)

class UrlImageField(models.ImageField):
    descriptor_class = UrlImageFileDescriptor

class TryField(models.Model):
    logo = UrlImageField(upload_to="victor")

custom_field = TryField.objects.create(logo="url_iof_image or File Instance") will work!!
Victor Castillo Torres
  • 10,581
  • 7
  • 40
  • 50
  • Alright. That's not quite what I wanted. However, the line `file = instance.__dict__[self.field.name]` in `FileDescriptor` raises an error now because the field name `logo` is not contained in the dict. – danijar Jul 26 '14 at 20:00
  • That's the property name of my model to which I attach my custom field. – danijar Jul 26 '14 at 20:05
  • I got it working, but the property gets set by string unexpectedly on every access. The current code is in the question. – danijar Jul 26 '14 at 20:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58078/discussion-between-victor-castillo-torres-and-danijar). – Victor Castillo Torres Jul 26 '14 at 20:51