Ok, I've figured the answer myself - but maybe someone will find it
useful...

When retrieving an ImageField, Django doesn't return the field but
rather the underlying ImageFieldFile. So say you have a custom
ImageField like this:

class Profile(models.Model)
        ...
        photo = models.RemoteImageField
        ...

When accessing an instance of Profile, (name it 'p') p.photo will
return the underlying ImageFieldFile. If you want it to have any non-
standard properties/methods you would need to extend the
ImageFieldFile as well. Fortunately, this is a freaking easy thing to
do - just inherit it in your the new class and add any new stuff you'd
like to see there.
The most important thing then is to let your extended ImageField
(RemoteImageField in my case) know that it shouldn't use the standard
ImageFieldFile, but rather your custom version. This is easily done by
setting attr_class of your extended ImageField to the class name of
your custom ImageFieldFile. Hope it's not too confusing. :) An example
is below:

class RemoteImageFile(files.ImageFieldFile):
        def _get_thumbnail_url(self):
                self._require_file()
                return self.storage.thumbnail_url(self.name)

        thumbnail_url = property(_get_thumbnail_url)

class RemoteImageField(models.ImageField):
        attr_class = RemoteImageFile

Then, p.thumbnail_url will work along with any standard ImageFile
properties.

Thanks,
Igor

On Jan 25, 2:12 am, Igor <igor.rubinov...@gmail.com> wrote:
> Hi all,
>
> I'm subclassing ImageField to add another function/property to the
> standard one (probably will add more later):
>
> from django.db import models
> class RemoteImageField(models.ImageField):
>         def thumbnail_url(self, name):
>                 self.storage.thumbnail_url(name)
>
> ...and then I do:
>
> class UserProfile(models.Model):
>         ....
>         profile_photo = RemoteImageField(upload_to='profile_pictures',
> blank=True, storage=pwcs())
>
> Now, if I check the type of profile_photo in UserProfile class it
> shows up as RemoteImageField. However, if I do the same thing in the
> view.py function (getting the profile via user.get_profile()), it is
> of ImageField type.
> Any ideas why this is happening and what can be done?
>
> Thanks a lot in advance,
> Igor

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to