投资者和管理者两类用户。一些用户(投资者)应该能够筛选出投资者类型,而其他人(经理)不应该。。。我有一张表格。用户表单信息。我只是想知道是否有一种简单的方法可以排除那些manager用户填写该字段。在
在我的模板中,我成功地将经理人排除在投资者类型字段之外。但是在提交他们的UserInfoForm时,会在模板中显示:investor_type
This field is 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_type = forms.CharField(max_length=4, required=False, widget=forms.Select(choices=choices), initial='0')
,但没用。在
在视图.py:在视图中尝试=失败。在
^{pr2}$我感谢你的帮助,并提前谢谢你。在
我会将“投资者类型”设置为:
然后为表单创建一个clean方法来检查用户的类型。如果是投资者而他们没有指定投资者类型抛出错误。如果是经理,就让它过去。在
您还需要确保您的配置文件模型允许investor_type为空和空: https://docs.djangoproject.com/en/dev/topics/db/models/#field-options
查看此文档了解表单的clean方法。 https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other
相关问题 更多 >
编程相关推荐