How can I save uploaded image's url in a column of database?

13 views
Skip to first unread message

prateek gupta

unread,
Jul 1, 2018, 1:01:20 PM7/1/18
to Django users
Hi All,

I am using Django2.0.6, Python3.6 and MySql.
I have a table Brand in db which have fields- id, name, description, image_url.
From django admin I am uploading an image to s3 bucket.

For this I have created a class Brand in models.py as below-
class Brand(models.Model):
    name
= models.CharField(max_length=60)
    description
= models.TextField(blank=True, null=True)

    @property
    def logo(self):
       
return get_logo(self.name, 'brand')

   
class Meta:
        managed
= False
        db_table = 'brand'

    def __str__(self):
       
return self.name

def get_logo(logo_name, folder):
    logo_name_with_ext
= '{}{}'.format(logo_name, '.png')
    logo_url
= '{}/{}'.format(IMAGE_PATH, os.path.join(folder, logo_name_with_ext))
   
return mark_safe('<img src="{}" height="20" />'.format(logo_url))

The image is uploading in S3 successfully.

But I want to save the same uploaded image's url to database also; inside 'image_url' column of Brand table.
How can I do that? What change should I make here?


Reply all
Reply to author
Forward
0 new messages