Let's say I have a model with a FileField:
class Foo(models.Model):
file = models.FileField(
upload_to='files'
)
What would be the best approach for getting an object of the model from the databse with the name of the file?
Something like:
try:
foo = Foo.objects.get(
file__name='bar.csv',
)
except Foo.MultipleObjectsReturned:
return something()
except Foo.DoesNotExist:
return something_else()
process(foo)
I guess I could add a file_name
field into the model and setting self.file_name = self.file.name
in Foo.save()
, but that seems cumbersome..?