I have a django model like this:
class Something(models.Model):
title = models.CharField(max_length=200, default=u'')
text = models.CharField(max_length=250, default=u'', blank=True)
photo = models.ImageField(upload_to=u'something')
def photo_thumb(self):
if self.photo:
return u'<img src="%s" />' % (settings.MEDIA_URL + '/thumbs/?h=64&w=80&c=50x0&p=' + self.photo.name)
else:
return u'(no photo)'
photo_thumb.short_description = u'Photo'
photo_thumb.allow_tags = True
photo_thumb.admin_order_field = 'photo'
def __unicode__(self):
return self.title;
class SomethingElse(models.Model):
name = models.CharField(max_length=200, default=u'')
foo = models.CharField(max_length=250, default=u'', blank=True)
photo = models.ImageField(upload_to=u'something_else')
def photo_thumb(self):
if self.photo:
return u'<img src="%s" />' % (settings.MEDIA_URL + '/thumbs/?h=64&w=80&c=50x0&p=' + self.photo.name)
else:
return u'(no photo)'
photo_thumb.short_description = u'Photo'
photo_thumb.allow_tags = True
photo_thumb.admin_order_field = 'photo'
def __unicode__(self):
return self.title;
I feel like this violates DRY, for obvious reasons. My question is, can I stick this somewhere else:
# ...
def photo_thumb(self):
if self.photo:
return u'<img src="%s" />' % (settings.MEDIA_URL + '/thumbs/?h=64&w=80&c=50x0&p=' + self.photo.name)
else:
return u'(no photo)'
photo_thumb.short_description = u'Photo'
photo_thumb.allow_tags = True
photo_thumb.admin_order_field = 'photo'
# ...
And then include it in relevant model classes with a single line of code? Or can photo_thumb be dynamically added to the appropriate classes somehow? I've tried classical and parasitic inheritance, but I may not be doing it right... I'm new to Django and fairly new to python also. Any help is appreciated.