Python 如何排除[vanilla django]表单中的字段?
存在两种类型的用户:投资者和管理者。一些用户(投资者)应该能够填写投资者类型,其他人(经理)不应该。。。我有一张表格。用户信息表单。我只是想知道是否有一种简单的方法可以排除那些manager用户填写该字段 在我的模板中,我成功地将经理排除在“投资者类型”字段之外。但在提交他们的用户信息表单时,会显示:Python 如何排除[vanilla django]表单中的字段?,python,django,django-forms,django-views,Python,Django,Django Forms,Django Views,存在两种类型的用户:投资者和管理者。一些用户(投资者)应该能够填写投资者类型,其他人(经理)不应该。。。我有一张表格。用户信息表单。我只是想知道是否有一种简单的方法可以排除那些manager用户填写该字段 在我的模板中,我成功地将经理排除在“投资者类型”字段之外。但在提交他们的用户信息表单时,会显示:investor\u type 此字段是必需的。在模板中 有没有什么方法可以让我把,required=只适用于投资者 class UserInfoForm(forms.Form): choi
investor\u type
此字段是必需的。
在模板中
有没有什么方法可以让我把,required=只适用于投资者
class UserInfoForm(forms.Form):
choices = (('0', "Foundation"), ('1', "Financial/Bank"), ('2', "Pension"), ('3', "Endowment"),\
('4', "Government Pension"), ('5', "Family Office"), ('6', "Insurance Co."),\
('7', "Corporation"), ('8', "Fund of Funds"), ('9', "Fund Manager"), ('10', "Asset Manager"), ('11', "Fundless Sponsor"))
first_name = forms.CharField(widget=forms.TextInput(attrs={'class':'input-text'}), max_length=30)
last_name = forms.CharField(widget=forms.TextInput(attrs={'class':'input-text'}), max_length=30)
email = forms.EmailField(widget=forms.TextInput(attrs={'class':'input-text'}))
about = forms.CharField(widget=forms.Textarea(attrs={'class':'input-text'}), required=False)
country = forms.CharField(max_length=50, widget=forms.Select(choices=countries.COUNTRIES))
avatar = forms.ImageField(required=False)
investor_type = forms.CharField(max_length=4, widget=forms.Select(choices=choices))
def save(self, user, type):
if type == 'manager':
profile = ManagerProfile.objects.get(user=user)
else:
profile = InvestorProfile.objects.get(user=user)
# // Tried this...
if profile.investor_type != self.cleaned_data['investor_type']:
profile.investor_type = self.cleaned_data['investor_type']
profile_edited = True
# // Failed
user_edited = False
if user.first_name != self.cleaned_data['first_name']:
user.first_name = self.cleaned_data['first_name']
user_edited = True
if user.last_name != self.cleaned_data['last_name']:
user.last_name = self.cleaned_data['last_name']
user_edited = True
if user.email != self.cleaned_data['email']:
user.email = self.cleaned_data['email']
user_edited = True
if user_edited:
user.save()
profile_edited = False
if profile.about != self.cleaned_data['about']:
profile.about = self.cleaned_data['about']
profile_edited = True
if profile.country != self.cleaned_data['country']:
profile.country = self.cleaned_data['country']
profile_edited = True
if profile_edited:
profile.save()
if self.cleaned_data['avatar']:
avatar = self.cleaned_data['avatar']
avatar.name = user.username + '.' + avatar.name.split('.')[-1]
profile.avatar.save(avatar.name, avatar)
我尝试了investor\u type=forms.CharField(max\u length=4,required=False,widget=forms.Select(choices=choices),initial=0')
,但没有成功
Views.py:在视图中尝试=失败
@login_required
def edit_profile(request, profile_type):
if profile_type == 'investor':
profile = InvestorProfile.objects.get(user=request.user)
elif profile_type == 'manager':
profile = ManagerProfile.objects.get(user=request.user)
context = base_context(request)
if request.method == 'POST':
notify = "You have successfully updated your profile."
user_info_form = UserInfoForm(request.POST, request.FILES)
if user_info_form.is_valid():
user_info_form.save(request.user, profile_type)
return HttpResponseRedirect(request.POST.get('next', '/profile/' + profile_type + '/' + request.user.username + '/'))
else:
initial = {}
initial['first_name'] = request.user.first_name
initial['last_name'] = request.user.last_name
initial['email'] = request.user.email
initial['about'] = profile.about
initial['country'] = profile.country
initial['about'] = profile.about
# // Tried this ...
if profile_type == 'investor':
initial['investor_type'] = profile.investor_type
elif profile_type == 'manager':
profile.investor_type.required = False
# // Failed
user_info_form = UserInfoForm(initial=initial)
context['user_info_form'] = user_info_form
context['profile_type'] = profile_type
context['profile'] = profile
return render_to_response('edit/profile.html', context, context_instance=RequestContext(request))
我感谢你的帮助,并提前向你表示感谢 我会将投资者类型设置为:
required=False
然后为表单创建一个干净的方法,检查用户类型。如果是投资者,而他们没有指定投资者类型,则抛出错误。如果是经理,就让他过去
您还需要确保您的配置文件模型允许投资者类型为空:
查看有关表单的clean方法的文档。
在投资者类型上设置required=False时发生了什么?哪一行产生了该异常?也看看我下面的答案,看看是否有帮助。我认为将其全局设置为required=False并使用clean方法可能是更好的方法。但是我找到了。这很有效,谢谢你。