Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

drop down menu - django model/modelForm - How to get dynamic choices in choiceField?

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.CharField(widget=FolderListDropDown())

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.

for 2. I tried this in my ModelForm

class test1Form(ModelForm):
    test_folder_ddl = forms.CharField(widget=FolderListDropDown()) 
    test_folder_ddl.choices = {some list}

I also tried class test1Form(ModelForm): test_folder_ddl = forms.ChoiceField(choices={some list})

and it would still render the default char field widget. Anyone know what i'm doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yay solved. after beating my head all day and going through all sorts of examples by people i got this to work.

basically i had the right idea with #2. The steps are
- Create a ModelForm of our model - override the default form field user for a models.CharField. i.e. we want to explcitly say use a choiceField.
- Then we have to override how the form is instantiated so that we call the thing we want to use to generate our dynamic list of choices
- then in our ModelAdmin make sure we explicitly tell the admin to use our ModelForm

class Test1(models.Model):
    test_folder_ddl  = models.CharField(max_length=100)


class Test1Form(ModelForm):
    test_folder_ddl = forms.choiceField()

    def __init__(self, *args, **kwargs):
       super(Test1Form, self).__init__(*args, **kwargs)
       self.fields['test_folder_ddl'].choices = utility.get_folder_list()

class Test1Admin(admin.ModelAdmin):
    form = Test1Form

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...