ParsaAi
ParsaAi

Reputation: 349

Django Model ImageField default value

I made a model which has an image field and it is allowed to be blank. How can I have a default image for the model when no image is set for it?

class Product(models.Model):
    picture = models.ImageField(blank=True)

Upvotes: 1

Views: 554

Answers (2)

Manoj Tolagekar
Manoj Tolagekar

Reputation: 1970

Do this:

class Product(models.Model):
        picture = models.ImageField(blank=True,default="Add image url which is you want")

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476574

You specify a default=… value [Django-doc]:

class Product(models.Model):
    picture = models.ImageField(blank=True, default='path/to/image.png')

the path is relative to the MEDIA_ROOT setting [Django-doc].

Upvotes: 2

Related Questions