我正在尝试更新一个模型对象,其中一个值来自查询集。对象创建工作完美 - 它使用传递给模板的查询集,然后在 POST 时将其保存到对象中。
我的问题:named 字段parent_jury
在模板中显示为空的选择字段。它不包含原始字段值。我如何让它将初始字段值设置为对象中的内容并允许用户从查询集中的其他值中进行选择?
但现在我正在尝试update
这个对象。这是代码:
#forms.py
def __init__(self, *args, **kwargs):
pk = kwargs.pop('pk')
yr = kwargs.pop('yr')
jr = kwargs.pop('jr')
self.customer_id = pk
self.court_year_id = yr
super().__init__(*args, **kwargs)
# This prints the correct value for the the object I'm trying to update
print("pr: ", Jury.objects.get(
jury_id=jr).parent_jury)
# This is my latest attempt to get the initial value (or any value) to
# be presented in the modelform/template.
self.fields['parent_jury'].initial = Jury.objects.get(
jury_id=jr).parent_jury
除了上面的代码,我还尝试__init__()
使用以下方法提取正确的值:
#forms.py (other attempts)
self.fields['parent_jury'] = forms.ChoiceField(
required = False,
choices = Jury.objects.filter(
customer_id=pk).filter(
court_year_id = yr),
initial = Juriy.objects.get(
jury_id=jr).parent_jury)
我的模板反映了我的 createview 模板(可能导致部分问题)。
#template.html
<tr><td>Parent Jury:</td><td><select name="parent_jury">
{% for item in form.parent_jury.field.queryset %}
<option value="{{ item }}">{{ item }}</option>
{% endfor %}
</select></td></tr>
我确实遇到过这个问题,但它使用了一个单独的过滤器类——我希望在__init__
.
很有可能我的问题实际上出在模板中 - 我遇到了这个,但仍然宁愿管理ModelForm
.
后续问题:
或者...是否可以将初始值与其他所有内容一起传递到模板中,然后将其编码到模板中?
我几乎想通了 - 这是forms.py
__ini__()
将权利传递choices
给模板的:
def __init__(self, *args, **kwargs):
pk = kwargs.pop('pk')
yr = kwargs.pop('yr')
jr = kwargs.pop('jr')
self.customer_id = pk
self.court_year_id = yr
super().__init__(*args, **kwargs)
print("pr: ", Juriy.objects.get(
jury_id=jr).parent_jury)
choices = list(Jury.objects.filter(
client_id = pk).filter(
court_year_id = yr).values_list('jury_name', 'jury_name'))
print("choices: ", choices)
self.fields['parent_jury'] = forms.ChoiceField(
choices = choices,
)
在.values_list()
我已经包含'jury_name'
两次以使列表项工作。只有一个值,下面的模板会抛出 err: not enough values to unpack (expected 2, got 1)
。传递两个解决了这个问题。
在模板中,我有:
<tr><td>Parent Jury:</td><td><select name="parent_jury">
{% for item in form.parent_jury %}
<option value="{{ item }}">{{ item }}</option>
{% endfor %}
</select></td></tr>
#output
Thingy1
Thingy1
Thingy2
Thingy2
Thingy3
Thingy3
如您所见,现在的问题是select/option
表单上的字段实际上重复了关键条目。每个键在下拉菜单中显示两次。
我还在模板底部运行了以下代码,结果正确:
{% for item in form.parent_jurisdiction %}
{{item}}
{% endfor %}
#output
Thingy1
Thingy2
Thingy3
我该如何解决?
我在下面的回答是错误的......虽然删除{{ item }}
标签实际上确实改变了选项以仅包含一组选择 - 它没有在 POST 中传递 - 相反,我在 POST 报告中得到了这个:
parent_jury '<option value='
所以,越来越近 - 视图是正确的,但所选的选项没有被传回。
为模板找到了正确的构造...答案已更新:
可以在此处找到模板的原始 SO 答案。
最后一块是在模板中。
原代码如下:
<option value="{{ item }}">{{ item }}</option>
这导致value
元素和label
元素都包含在选项列表中。由于两者具有相同的值,因此它们(thingy
或另一个thingy1
)在 POST 中具有相同的结果。select/option/iteration
可以在此处找到此构造。
#update code
<tr><td>Parent Jurisdiction:</td><td><select name="parent_jury">
{% for val, item in form.fields.parent_jury.choices %}
<option value="{{ item }}"{% if form.fileds.parent_jury.value == val%} selected{% endif %}>{{ item }}</option>
{% endfor %}
</select></td></tr>
作为后续说明,在选项中使用查询集对象需要两者:
# passing queryset to select/option
<tr><td>Parent Jury:</td><td><select name="parent_jury">
{% for item in form.parent_jury.field.queryset %}
<option value="{{ item }}">{{ item }}</option>
{% endfor %}
</select></td></tr>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句