Adding FilePathField

485 views
Skip to first unread message

Adrian Holovaty

unread,
Sep 29, 2005, 12:11:31 AM9/29/05
to django-d...@googlegroups.com
I came across this blog entry today:

http://www.socialistsoftware.com/?p=12

The author suggests a "ServerFileField", which you would instantiate
with a given filepath on the system, and it would be represented in
the admin by a select box listing all the files in that directory.

For example, this...

FilePathField('/home/media/')

...would generate a select box with all the files within /home/media/.

This is a great idea, and we should implement it right away. But
there's one question that needs to be answered: How should it choose
the files? A couple of choices:

* Accept "glob" syntax, e.g. "/home/media/*", which would follow Unix
glob conventions -- e.g. just the files directly within the media
directory.

* Automatically include all files within the directory, recursively.

* Both? If so, what's the syntax?

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Sune Kirkeby

unread,
Sep 29, 2005, 3:00:19 AM9/29/05
to django-d...@googlegroups.com
On 9/29/05, Adrian Holovaty <holo...@gmail.com> wrote:
> This is a great idea, and we should implement it right away. But
> there's one question that needs to be answered: How should it choose
> the files? A couple of choices:

> * Both? If so, what's the syntax?

How about a subset of the find(1) tests and operators?

/s

Moof

unread,
Sep 29, 2005, 7:19:47 AM9/29/05
to django-d...@googlegroups.com
Adrian Holovaty wrote:

> This is a great idea, and we should implement it right away. But
> there's one question that needs to be answered: How should it choose
> the files? A couple of choices:
>
> * Accept "glob" syntax, e.g. "/home/media/*", which would follow Unix
> glob conventions -- e.g. just the files directly within the media
> directory.

Yes, that makes a lot of sense. That way I can display all the .gif files in
a directory if I so wished.

Also, make sure the admin view has the possibility of having it as a
multi-line select box, i could, potentially, be a big list.

> * Automatically include all files within the directory, recursively.

No, but there's no reaon not to have recursive=False as part of the syntax

Also, I'd suggest that rather than returning the file from the field you
return a path object, which makes life much easier as far as coping with
things like metadata an so on goes. There are several path objects out
there, but by far the most ubiquitous is Jason Rendorff's one:

<http://jorendorff.com/articles/python/path/>

I believe it was considered for inclusion in 2.5 at one stage, not sure how
that's continued.

You might wish to have a look and see whether path's built-in globbing
options are something you wish to just lift.

Moof
--
Giles Antonio Radford, alias Moof
"Too old to be a chicken and too young to be a dirty old man"
Serving up my ego over at <http://metamoof.net/>

Manuzhai

unread,
Sep 29, 2005, 11:06:25 AM9/29/05
to django-d...@googlegroups.com
I love the idea of a FilePathField. The globbing thing seems nice.

I'm not sure about using Jason Orendorff's path module, since it's Yet
Another Dependency. I'm not sure it adds that much value (and it does
add some learning curve for those not familiar with it).

Regards,

Manuzhai

Amit Upadhyay

unread,
Sep 29, 2005, 11:19:22 AM9/29/05
to django-d...@googlegroups.com
On 9/29/05, Manuzhai <manu...@gmail.com> wrote:
I'm not sure about using Jason Orendorff's path module, since it's Yet
Another Dependency. I'm not sure it adds that much value (and it does
add some learning curve for those not familiar with it).

Its a single python module, very liberally licensed so it can include it in the distribution, something like:
try: import path
except ImportError: import django.core.path as path
And its a charm to use, not at all a learning curve and really pythonic, so we should not limit its usage on these two points, there may be other reasons not to use it tho, which I have no opinion about.

--
Amit Upadhyay
Blog: http://www.rootshell.be/~upadhyay
+91-9867-359-701

ska...@gmail.com

unread,
Sep 30, 2005, 10:22:16 AM9/30/05
to Django developers
Hi everyone
I was the one that made that blog post, so I figured I would chime in.

I think getting files recursively would be good as long as you can pass
an option to not do that. I also think it would be cool if you could
pass a regex string to match certain files. So you could list all the
zip files in a dir.

theFile = meta.FilePathField(path='/path/to/files', match='*.zip',
recursive=true)

I had not heard of the Path module before. It seems like it would do
everything we wanted with a bit less code than os.listdir. I guess I
really don't have much of an opinion if this gets used or not.

ska...@gmail.com

unread,
Oct 3, 2005, 9:35:06 PM10/3/05
to Django developers
I just submited my first stab at this. Please give it a try and let me
know what you think.

http://code.djangoproject.com/ticket/589

ska...@gmail.com

unread,
Oct 11, 2005, 9:42:53 PM10/11/05
to Django developers
So ...

Any one had a chance to look at this yet?

Adrian Holovaty

unread,
Oct 12, 2005, 12:18:20 AM10/12/05
to django-d...@googlegroups.com
On 10/11/05, ska...@gmail.com <ska...@gmail.com> wrote:
> Any one had a chance to look at this yet?

I just added it, and I've updated the docs. Thanks again for the idea and patch!

http://code.djangoproject.com/changeset/846
http://www.djangoproject.com/documentation/model_api/#field-types
Reply all
Reply to author
Forward
0 new messages