开发者

django admin inline formset override save

I have a mo开发者_开发百科del that looks like this:

class ProjectImage(models.Model):
    big_thumb = ThumbnailField(upload_to='profiles', size=(500, 500))
    med_thumb = ThumbnailField(upload_to='profiles', size=(300, 300))
    small_thumb = ThumbnailField(upload_to='profiles', size=(100, 100))

I associate ProjectImage with a Project as a TabularInline. In the admin, I'd like to be able to say if the medium and/or small thumbnails were not provided, use the same image as the big thumbnail. However, I'm having a hard time figuring out how to specify this behavior.


You have two options in this case, you can either over-ride ProjectImage's save defintion and have it copy big_thumb to med_thumb and small_thumb if they are None, or just create a simple model definition to display each field.

def get_med_thumb_url(self):
    if self.med_thumb is None:
         return self.big_thumb.url

    return self.med_thumb.url

And do the same for small_thumb That keeps you from fidgeting with the save method and having to copy things over and waste space.

I can't remember if ImageField has a get_FOO_url type definition but if it does you could always over-ride that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜