【问题标题】:Django initial value of choicefielddjango的choicefield初始值
【发布时间】:2011-12-13 20:43:17
【问题描述】:

我遇到了一个奇怪的问题,我似乎无法在 django 的表单中设置其中一个字段的初始值。

我的模型字段是:

section = models.CharField(max_length=255, choices=(('Application', 'Application'),('Properly Made', 'Properly Made'), ('Changes Application', 'Changes Application'), ('Changes Approval', 'Changes Approval'), ('Changes Withdrawal', 'Changes Withdrawal'), ('Changes Extension', 'Changes Extension')))

我的表单代码是:

class FeeChargeForm(forms.ModelForm):
    class Meta:
        model = FeeCharge
        # exclude = [] # uncomment this line and specify any field to exclude it from the form

    def __init__(self, *args, **kwargs):
        super(FeeChargeForm, self).__init__(*args, **kwargs)
        self.fields['received_date'] = forms.DateField(('%d/%m/%Y',), widget=forms.DateTimeInput(format='%d/%m/%Y', attrs={'class': 'date'}))
        self.fields['comments'].widget.attrs['class']='html'
        self.fields['infrastructure_comments'].widget.attrs['class']='html'

我的查看代码是:

form = FeeChargeForm(request.POST or None)
form.fields['section'].initial = section

其中 section 是传递给函数的 url var。我试过了:

form.fields['section'].initial = [(section,section)]

也没有运气:(

有什么想法我做错了什么,或者有更好的方法从 url var 设置此选择字段的默认值(在表单提交之前)?

提前致谢!

更新:这似乎与 URL 变量有关。如果我使用:

form.fields['section'].initial = "Changes Approval"

它工作 np.. 如果我 HttpResponse(section) 它的输出正确。

【问题讨论】:

    标签: python django


    【解决方案1】:

    更新 尝试转义您的网址。以下 SO 答案和文章应该会有所帮助:

    How to percent-encode URL parameters in Python?

    http://www.saltycrane.com/blog/2008/10/how-escape-percent-encode-url-python/

    尝试如下设置该字段的初始值,看看是否可行:

    form = FeeChargeForm(initial={'section': section})
    

    我假设当用户发布表单时您会做很多其他事情,因此您可以使用以下方式将 POST 表单与标准表单分开:

    if request.method == 'POST':
        form = FeeChargeForm(request.POST)
    form = FeeChargeForm(initial={'section': section})
    

    【讨论】:

    • 尝试过也没有运气:( - 还有其他想法吗?
    • 尝试对您的 URL 进行编码...在我的答案中添加了几个链接,这可能会有所帮助。
    【解决方案2】:

    问题是request.POSTinitial={'section': section_instance.id}) 一起使用。这是因为request.POST 的值总是覆盖参数initial 的值,所以我们必须把它分开。我的解决方案是使用这种方式。

    在views.py中:

    if request.method == "POST":
        form=FeeChargeForm(request.POST) 
    else:
        form=FeeChargeForm() 
    

    在forms.py中:

    class FeeChargeForm(ModelForm):
        section_instance = ... #get instance desired from Model
        name= ModelChoiceField(queryset=OtherModel.objects.all(), initial={'section': section_instance.id})
    

    --------- 或 ----------

    在views.py中:

    if request.method == "POST":
        form=FeeChargeForm(request.POST) 
    else:
        section_instance = ... #get instance desired from Model
        form=FeeChargeForm(initial={'section': section_instance.id}) 
    

    在forms.py中:

    class FeeChargeForm(ModelForm):
        name= ModelChoiceField(queryset=OtherModel.objects.all())
    

    【讨论】:

      猜你喜欢
      • 2014-07-07
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      相关资源
      最近更新 更多