Reputation: 2467
Okay so I added a field to a model called college.
Here is the new field:
logo = models.ImageField(upload_to="logos", default="static/images.default.png")
So I have a few images in my media folder, but how do I associate them with certain images in the class.
Would I say Harvard.logo = ?
Would the ? be the image path. What exactly is the image path? Using Pillow.
Upvotes: 2
Views: 161
Reputation: 6647
Address of the ImageField
type starts by MEDIA_ROOT
path and then directory that determined in upload_to
parameter, also on the db save path similar upload_to_dir/file_name.ext
format.
Your default
parameter must start with uploat_to
value, change this similar below:
logo = models.ImageField(upload_to="logos", default="logos/images.default.png")
And for select an image that is related to college
value you can override save
method of your model:
class MyModel(models.Model):
logo = models.ImageField(upload_to="logos", default="logos/images.default.png", blank=None)
college = models.IntegerField(choices=[(1, 'Harvard'), (2, 'MIT'), (3, 'Oxford')])
def save(self, *args, **kwargs):
if self.logo is None:
if self.college == 1:
self.logo = 'logos/Harvard.png'
if self.college == 2:
self.logo = 'logos/MIT.png'
if self.college == 3:
self.logo = 'logos/Oxford.png'
super(MyModel, self).save(*args, **kwargs)
Notice: These default images must already exists in logos
directory or copy to this.
Upvotes: 2