[英]Django UpdateView extend the Form field choices
我有一個Django表單,其中一個名為ipts
的字段默認情況下具有內容choices=(('', '-'*20),)
。
在UpdateView
我使用更多選項擴展了此列表。 因此,在get_context_data
我獲得了表單並擴展了該列表。
choices = form.fields['ipts'].choices
choices = choices.extend( [('IPTS-123', 'IPTS-454545'),] )
為了確保正確填充選擇,在render_to_response
方法中,我打印了form.fields['ipts'].choices
並且得到了我期望的結果:
[('', '--------------------'), ('IPTS-123', 'IPTS-454545')]
但是,模板沒有填充這些選擇! 僅初始值可供選擇: ('', '--------------------')
有什么想法如何在基於類的視圖中擴展選擇字段?
歡迎提出建議。 謝謝。
編輯:
這里查看代碼:
class ProfileReductionUpdate(LoginRequiredMixin, SuccessMessageMixin,
UpdateView):
'''
'''
model = UserProfile
form_class = UserProfileReductionForm
template_name = 'users/profile_reduction_form.html'
# fields = '__all__'
success_url = reverse_lazy('index')
success_message = "Your Profile for the Reduction was updated successfully."
def get(self, request, *args, **kwargs):
print("*** get")
return super(ProfileReductionUpdate, self).get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
print("*** get_context_data")
context = super(ProfileReductionUpdate, self).get_context_data(**kwargs)
from pprint import pprint
# pprint(context)
import copy
form = context['form']
print("Form:")
pprint(form)
choices = form.fields['ipts'].choices
choices = choices.extend( [('IPTS-123', 'IPTS-454545'),] )
pprint(form.fields['ipts'].choices)
# form.fields['ipts'].choices = choices
context['form'] = form
pprint(context['form'].fields['ipts'].choices)
return context
def render_to_response(self, context, **response_kwargs):
print("*** render_to_response")
from pprint import pprint
pprint(context)
pprint(response_kwargs)
form = context['form']
print("Form:")
pprint(form)
print("Form ipts choices:")
pprint(form.fields['ipts'].choices)
pprint(form.fields['ipts']._choices)
# pprint(dir(form.fields['ipts']))
return super(ProfileReductionUpdate, self).render_to_response(context, **response_kwargs)
這里的輸出:
*** get
[22/Nov/2017 10:22:14] DEBUG [server.apps.users.models:60] QuerySet
*** get_context_data
Form:
<UserProfileReductionForm bound=False, valid=Unknown, fields=(ipts;experiment)>
[('', '--------------------'), ('IPTS-123', 'IPTS-454545')]
[('', '--------------------'), ('IPTS-123', 'IPTS-454545')]
*** render_to_response
{'form': <UserProfileReductionForm bound=False, valid=Unknown, fields=(ipts;experiment)>,
'object': <UserProfile: rhf>,
'userprofile': <UserProfile: rhf>,
'view': <server.apps.users.views.ProfileReductionUpdate object at 0x7f0d29d14b70>}
{}
Form:
<UserProfileReductionForm bound=False, valid=Unknown, fields=(ipts;experiment)>
Form ipts choices:
[('', '--------------------'), ('IPTS-123', 'IPTS-454545')]
[('', '--------------------'), ('IPTS-123', 'IPTS-454545')]
編輯2:
形成:
class UserProfileReductionForm(forms.ModelForm):
'''
'''
def __init__(self, *args, **kwargs):
super(UserProfileReductionForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_class = 'form-horizontal'
self.helper.layout.append(Submit('submit', 'Save'))
self.helper.layout.append(Button('cancel', 'Cancel',
css_class='btn-default',
onclick="window.history.back()"))
class Meta:
model = UserProfile
# exclude = ['user']
fields = ['ipts', 'experiment']
模型:
ipts = models.CharField(
"Integrated Proposal Tracking System (IPTS)",
max_length=20,
blank=True,
choices=(('', '-'*20),)
好! 我想到了!
要填充的字段是: form.fields['ipts'].widget.choices
而不是: form.fields['ipts'].choices
希望對別人有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.