Opened 7 years ago

Closed 5 years ago

#11228 closed New feature (wontfix)

FieldFile with check if file exists.

Reported by: hersonls Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no


Many people ask and also has the need to verify that the file is sent to the server is really in its place.

This need arises because they are made unnecessary requests for lack of the file in media server and also for reasons of aesthetics, because the lack of image files or result in a different box with alt attribute, or result in a 404 not found.

The class FieldFile not give the developer the possibility to check if file exists and that is the developers create a method in a class model which will check if file exists on the media server or not.

I created a patch where it created a method for class FieldFile call is_exists which checks if the file exists. This method is valid for all inheritance of FieldFile class.

For example,

{% if object.file.is_exists %}<a href="{{ MEDIA_URL }}{{ object.file }}">download this file</a>{% endif %}
{% if object.image.is_exists %}<img src="{{ MEDIA_URL }}{{ object.image }}" alt="My image" />{% endif %}

Attachments (3) (676 bytes) - added by hersonls 7 years ago.
Fix the patch
file2.txt (11 bytes) - added by anonymous 4 years ago.
file2.2.txt (11 bytes) - added by anonymous 4 years ago.

Download all attachments as: .zip

Change History (7)

Changed 7 years ago by hersonls

Fix the patch

comment:1 Changed 7 years ago by dc

  • Needs documentation set
  • Needs tests set
  • Patch needs improvement set

If path is stored in database and actual file not exists then data is corrupted and better not create workarounds but fix it immediately.

Anyway os.path.exists will not work with non-local file systems better use

comment:2 Changed 7 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Design decision needed

And "is_exists" seems ugly. Why not just "exists"?

But yeah, I'm dubious as whether this functionality is important enough to include in the api.

comment:3 Changed 5 years ago by julien

  • Severity set to Normal
  • Type set to New feature

comment:4 Changed 5 years ago by aaugustin

  • Easy pickings unset
  • Resolution set to wontfix
  • Status changed from new to closed
  • UI/UX unset

Like Chris, I doubt this would be generally useful. If you're losing files, you're got bigger problems than broken links in your templates!

Furthermore, it's trivial to subclass FileField to add this method if you need it.

Changed 4 years ago by anonymous

Changed 4 years ago by anonymous

Note: See TracTickets for help on using tickets.
Back to Top