I need to define a field in my Model, which is supposed to host a valid dir path on server-side. Basically just a string which should be:
1) a formally valid unix-like dir path
2) an existing dir path
Tried with FilePathField
with options allow_files=False, allow_folders=True
.
But when I try to create a new instance of the model from the django admin CRUD, I'm getting an error claiming that the initial value of the field (which is by default an empty string) is a not existing path...
I have a feeling this is not the right way. Maybe another field type could be more suitable? Maybe it should be just a simple string? (in this case, shall I be able to define correctly the required validators?)
Thanks for any hint,
Thomas
After some tests, I realized this model field type actually is used for fields which are assigned a value by browsing among files/dirs that already exist in the filesystem. Hence, the error was due to the path parameter I used (which I chose randomly) that didn't exist in the filesystem.
However, this Model field still fits my purposes, because having to choose a directory that already exists, certainly forces the user to assign a value which satisfies both validation points above mentioned.
There is even the possibility to make a "deep browsing", by including the entire sub dir tree. Just use the recursive=True
option.
So
repository = models.FilePathField("repo_root/", allow_files=False, allow_folders=True, recursive=True)
will do the trick.
For sub trees with many directories the page might become unresponsive.
Thomas