Validate form A's data by usuing form B's data
So I have these two forms. I would like to be able to access the data in form env_form when I am checking add_uRG for my other form. Is it possible to do this? My env form is a very common form through out my app so I would like to keep it separate instead of including it on every form.
class env_form(forms.Form):
env = forms.ChoiceField(choices=ENV, required=True)
class add_uRG(forms.Form):
开发者_如何学运维 user = forms.CharField(max_length=50)
group = forms.CharField(required=True)
role = forms.CharField(required=True)
def clean_user(self):
post_user = self.cleaned_data['user']
post_env = self.cleaned_data['env']
c = User.objects.filter(user__contains=post_user, env__contains=post_env ).count()
if (c == 0):
raise forms.ValidationError(u"User Not Found.")
else:
user_info = User.objects.filter(user__contains=post_user).values('password').distinct().count()
user_env = User.objects.filter(user__contains=post_user).values('env').distinct().count()
if not (user_env == user_info):
raise forms.ValidationError(u'User is using same password')
return(post_user)
If I'm reading your question correctly, I think that form inheritance could be used here.
class env_form(forms.Form):
env = forms.ChoiceField(choices=ENV, required=True)
class add_uRG(env_form):
user = forms.CharField(max_length=50)
group = forms.CharField(required=True)
role = forms.CharField(required=True)
def clean_user(self):
post_user = self.cleaned_data['user']
post_env = self.cleaned_data['env']
c = User.objects.filter(user__contains=post_user, env__contains=post_env ).count()
if (c == 0):
raise forms.ValidationError(u"User Not Found.")
else:
user_info = User.objects.filter(user__contains=post_user).values('password').distinct().count()
user_env = User.objects.filter(user__contains=post_user).values('env').distinct().count()
if not (user_env == user_info):
raise forms.ValidationError(u'User is using same password')
return(post_user)
def __init__(self, *args, **kwargs):
super(env_form, self).__init__(*args, **kwargs)
class SomeOtherForm(env_form):
some_field = forms.CharField()
def __init__(self,*args,**kwargs):
super(env_form, self).__init__(*args,**kwargs)
You could assign a property on your add_uRG form after you validate the env_form, or you could pass the value to the add_uRG form as a parameter.
#forms.py
class AdduRGForm(forms.Form):
def __init__(self, *args, **kwargs):
super(AdduRGForm, self).__init__(*args, **kwargs)
self.env = None
#my_view.py
def my_view(request):
env_form = env_form(request.POST or None)
if request.POST:
if env_form.is_valid():
add_uRG_form = AdduRGForm(request.POST or None)
add_uRG_form.env = env_form.cleaned_data.get('env')
if add_uRG_form.is_valid():
#do something else
return render_to_response('template.html',
{'env_form' : env_form, 'add_uRG_form' : add_uRG_form})
I'm not exactly sure of your workflow, so if these forms don't exist in the same view, like if you're processing the env_form first, and then going to another view and needing the value someone previously selected, you could pass in the request to env_form, and set the selection in session, which you could pick up again in the second form using the method I outlined.
精彩评论