django 模型/modelForm - 如何在choiceField 中获得动态选择? [英] django model/modelForm - How to get dynamic choices in choiceField?

查看:26
本文介绍了django 模型/modelForm - 如何在choiceField 中获得动态选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验 django 和内置管理界面.

i'm experimenting with django and the builtin admin interface.

我基本上希望在管理 UI 中有一个下拉字段.下拉选项应该是指定目录中的所有可用目录.
如果我定义这样的字段:

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.

有谁知道如何显示文件夹?

我也试过

test_folder_list = models.charField(max_length=100, choices=SOME_LIST)

其中 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.

提前致谢.

更新:
经过一些思考和研究,我发现我想要的可能是
1. 创建我自己的基于 forms.ChoiceField 的小部件

2.在呈现给客户端时将我的文件夹列表传递给选择列表

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

对于 1. 我尝试了自定义小部件.我的模型看起来像

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)

然后我在我的modelForm中做了这个

then i did this in my modelForm

class test1Form(ModelForm):
    test_folder_ddl  = forms.CharField(widget=FolderListDropDown())

它似乎不起作用.我的意思是 django 不想使用我的小部件,而是呈现您在使用 CharField 时获得的默认文本输入.

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.

对于 2. 我在我的 ModelForm 中尝试了这个

for 2. I tried this in my ModelForm

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

我也试过了类 test1Form(ModelForm):test_folder_ddl = forms.ChoiceField(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?

推荐答案

Yay 解决了.经过一整天的打击并经历了人们的各种例子后,我终于开始工作了.

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

基本上我对#2 的想法是正确的.步骤是
- 创建我们模型的 ModelForm- 覆盖models.CharField 的默认表单字段用户.即我们想明确地说使用choiceField.
- 然后我们必须覆盖表单的实例化方式,以便我们调用我们想要使用的东西来生成我们的动态选择列表
- 然后在我们的 ModelAdmin 中确保我们明确告诉管理员使用我们的 ModelForm

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

这篇关于django 模型/modelForm - 如何在choiceField 中获得动态选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆