Default image for ImageField in Django's ORM

后端 未结 6 1736
夕颜
夕颜 2020-12-03 01:22

I\'m using an ImageField to store profile pictures on my model.

How do I set it to return a default image if no image is defined?

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 01:23

    You could theoretically also use a function within your model definition. But I dont know whether this is good practice:

    class Image(model.Models):
        image = ....
    
        def getImage(self):
            if not self.image:
                # depending on your template
                return default_path or default_image_object
    

    and then within a template

       
    

    This gives you a great deal of flexibility for the future...

    I'm using sorl for thumbnails generation with great success

提交回复
热议问题