Here is the code…use this code in your models.py file

import os
from datauri import DataURI
import short_url
from django.utils.safestring import mark_safe
from django.utils.html import format_html

Class YourClassName(models.Model):
     field_name = models.FileField(upload_to = settings.YOUR_PATH, blank=True, null=True, storage=settings.UPLOAD_STORAGE_PATH, validators=[validate_file_size,validate_file_extension])
@property
def field_name_image(obj):
    if os.path.isfile(obj.field_name.path):
          t_data = DataURI.from_file(obj.field_name.url)
    else:
          t_data = ''
    t_data = f"{t_data}"
    return mark_safe("{t_data}".format(t_data=t_data))
   

here is my settings.py file code

UPLOAD_STORAGE_PATH = FileSystemStorage(location=UPLOAD_ROOT, base_url=UPLOAD_ROOT)
YOUR_PATH = os.environ.get('YOUR_PATH')

LEAVE A REPLY

Please enter your comment!
Please enter your name here