How to filter image field by file name in django

I am adding ImageField to my model, for example

class UserImage(models.Model):
    photo = models.ImageField(upload_to='target_path')
    ....

after saving the image, say "a.jpg", then I want the user filename "a.jpg" to filter the model, what should I write:

UserImage.objects.filter(photo.filename='a.jpg')
....
+5
source share
1 answer

Your suggestion will give you an error. Try instead:

UserImage.objects.filter(photo='a.jpg')

Edit: Django adds the upload_path to the file name. Then the request should do something like this, for example:

UserImage.objects.filter(photo='images/users/photos/a.jpg')

+12
source

All Articles