How to add custom property or method to ImageFieldFile object with minimal pain?
For instance i have this model:
class SomeModelWithImage(models.Model):
image = models.ImageField()
and a class:
class Custom(object):
def __repr__(self):
return "<Custom object>"
I want to somehow override ImageFieldFile object so it will have a calculated method that returns my Custom object, like:
>> from models import SomeModelWithImage
>> i = SomeModelWithImage.objects.all()[0]
>> i.image
<ImageFieldFile: /path/to/file>
>> i.image.custom_text
<Custom object>
UPD. i changed some object names, to make problem abstract and to not confuse you
In Django, one model field always corresponds to one database column, so what you want isn’t possible. Also, your class names should always be singular, because an object of class ‘Image’ should just be a single image. I think you might want something more like the following:
EDIT:
If you want a calculated field/method that doesn’t need extra database information, just subclass models.ImageField: