[Answered ]-How to get ImageField url when using QuerySet.values?

1👍

You can modify Queryset dictionary values like this…

models.py

class DEMOClass(models.Model):
      field1 = models.CharField(max_length=255, null=True,blank=True)
      field2 = models.ImageField(upload_to='Images')

      def __str__(self):
            return self.field1

views.py

def InsertData(request):
    data = DEMOClass.objects.values('field1','field2')
    print('------ Before ------')
    print(data)        
    for i in data:
        i.update({'field2':str(request.build_absolute_uri(i['field2']))})
    print('------ After ------')
    print(data) 

Output

------ Before ------
<QuerySet [{'field1': 'test', 'field2': 'Images/download_rBWYYCh.jpg'}]>
------ After ------
<QuerySet [{'field1': 'test', 'field2': 'http://127.0.0.1:8000/Images/download_rBWYYCh.jpg'}]>

Leave a comment