KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
i'm experimenting with django and the builtin admin interface. I basically want to have a field that is a drop down in the admin UI. The drop down choices should be all the directories available in a specified directory. If i define a field like this: test_folder_list = models.FilePathField(path=/some/file/path) it shows me all the files in the directory, but not the directories . Does anyone know how i can display the folders? also i tried doing test_folder_list = models.charField(max_length=100, choices=SOME_LIST) where SOME_LIST is a list i populate using some custom code to read the folders in a directory. This works but it doesn't refresh. i.e. the choice list is limited to a snapshot of whatever was there when running the app for the first time. thanks in advance. update: after some thinking and research i discovered what i want may be to either 1. create my own widget that is based on forms.ChoiceField or 2. pass my list of folders to the choice list when it is rendered to the client for 1. i tried a custom widget. my model looks like class Test1(models.Model): test_folder_ddl = models.CharField(max_length=100) then this is my custom widget: class FolderListDropDown(forms.Select): def __init__(self, attrs=None, target_path): target_folder = '/some/file/path' dir_contents = os.listdir(target_folder) directories = [] for item in dir_contents: if os.path.isdir(''.join((target_folder,item,))): directories.append((item, item),) folder_list = tuple(directories) super(FolderListDropDown, self).__init__(attrs=attrs, choices=folder_list) then i did this in my modelForm class test1Form(ModelForm): test_folder_ddl = forms.C
Tags (comma-separated)
Save Edits
Cancel