Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango model/modelForm - How to get dynamic choices in choiceField?
    primarykey
    data
    text
    <p>i'm experimenting with django and the builtin admin interface.</p> <p>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.<br> If i define a field like this:</p> <pre><code>test_folder_list = models.FilePathField(path=/some/file/path) </code></pre> <p>it shows me all the <em>files</em> in the directory, but not the <em>directories</em>. </p> <p><strong>Does anyone know how i can display the folders?</strong> </p> <p>also i tried doing</p> <pre><code>test_folder_list = models.charField(max_length=100, choices=SOME_LIST) </code></pre> <p>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.</p> <p>thanks in advance.</p> <hr> <p>update:<br> after some thinking and research i discovered what i want may be to either<br> <strong>1.</strong> create my own widget that is based on forms.ChoiceField<br> or<br> <strong>2.</strong> pass my list of folders to the choice list when it is rendered to the client</p> <p>for <strong>1.</strong> i tried a custom widget. my model looks like </p> <pre><code>class Test1(models.Model): test_folder_ddl = models.CharField(max_length=100) </code></pre> <p>then this is my custom widget: </p> <pre><code>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) </code></pre> <p>then i did this in my modelForm</p> <pre><code>class test1Form(ModelForm): test_folder_ddl = forms.CharField(widget=FolderListDropDown()) </code></pre> <p>and it didn't seem to work.What i mean by that is django didn't want to use my widget and instead rendered the default textinput you get when you use a CharField.</p> <p>for <strong>2.</strong> I tried this in my ModelForm</p> <pre><code>class test1Form(ModelForm): test_folder_ddl = forms.CharField(widget=FolderListDropDown()) test_folder_ddl.choices = {some list} </code></pre> <p>I also tried class test1Form(ModelForm): test_folder_ddl = forms.ChoiceField(choices={some list})</p> <p>and it would still render the default char field widget. <strong>Anyone know what i'm doing wrong?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload