[Django]-Is it possible to query Django objects by their FileField's url attribute

3👍

If you know exactly what you want to match you could do;

MediaLibraryFile.objects.filter(media_file__exact='some_key')

Or alternatively you could do a query like;

MediaLibraryFile.objects.filter(media_file__contains='some')

Just beware, the above is case sensitive. If you don’t know the case, you can do filter(media_file__icontains='something')

The docs for field lookups are here; https://docs.djangoproject.com/en/2.0/topics/db/queries/#field-lookups

0👍

I solved it by annotating the query value and making sure that the annotated value ends with the name of the file

query_value = "fullUrl/filename.pdf"
# file name => filename.pdf

media = MediaLibraryFile.objects.annotate(query_value=Value(query_value)).filter(query_value__endswith=F("media_file"))

Leave a comment