FilePathField and stale choices

126 views
Skip to first unread message

Daniel Swarbrick

unread,
Jul 6, 2011, 4:41:57 PM7/6/11
to Django developers
This problem was raised at least twice [1] [2] back in 2009 on django-
users, and neither post was answered.

If FilePathField is used declaratively for a form's field(s), it does
not get called for each instantiation of that form - only the first
time the form is used. Consequently, the choices may show files that
have since been deleted, and fail to show files that have since been
added to the directory. One of the users who raised this issue wrote
that he has to reload his server... to get updated files to show.

Should the docs perhaps reflect this, and maybe advise people to
dynamically add FilePathFields to their form.__init__() if they want
them to always be minty fresh?

Or can we perhaps even add an option to FilePathField that would
refresh the choices each time? I'm not entirely sure how that would
work... proxy function maybe?

[1]: http://groups.google.com/group/django-users/browse_thread/thread/6778fa138b848996
[2]: http://groups.google.com/group/django-users/browse_thread/thread/403d872cf9433905

Luke Plant

unread,
Jul 6, 2011, 7:04:59 PM7/6/11
to django-d...@googlegroups.com
On 06/07/11 21:41, Daniel Swarbrick wrote:

> Or can we perhaps even add an option to FilePathField that would
> refresh the choices each time? I'm not entirely sure how that would
> work... proxy function maybe?

That sounds like a reasonable idea to me, since doing it by default
could impose a serious performance hit. In it's most naive form (i.e.
making the 'choices' attribute a lazy list using
django.utils.functional.lazy) this wouldn't be too hard to implement.

However, that would mean that *every* time it was accessed it would have
to walk the file system to get the choices, which could easily get
crazy. A better solution would add some level of caching. This sounds
like we need an option that specifies the number of seconds to cache
for. It would be OK to require that there is a cache backend active for
this to work. We could have a single setting that controls the two e.g.
cache_paths_for - if it was None (the default) then it would cache forever.

Feel free to open a ticket for this.

Luke

--
A mosquito cried out in pain:
"A chemist has poisoned my brain!"
The cause of his sorrow
was para-dichloro-
diphenyltrichloroethane

Luke Plant || http://lukeplant.me.uk/

Daniel Swarbrick

unread,
Jul 7, 2011, 8:28:51 AM7/7/11
to Django developers
On Jul 7, 1:04 am, Luke Plant <L.Plant...@cantab.net> wrote:
> That sounds like a reasonable idea to me, since doing it by default
> could impose a serious performance hit. In it's most naive form (i.e.
> making the 'choices' attribute a lazy list using
> django.utils.functional.lazy) this wouldn't be too hard to implement.
>
> Feel free to open a ticket for this.
>

Done - https://code.djangoproject.com/ticket/16429
Reply all
Reply to author
Forward
0 new messages