FormView CBV根据请求更改ModelForm [英] FormView CBV change ModelForm per request

查看:88
本文介绍了FormView CBV根据请求更改ModelForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个向导来填充DataBase中的数据。
我尝试session.Wizard与成功加上我不认为这是ti需要。
如果我错了,请纠正我



所以基本上我的数据库有一些需要填写的表,
因为关节需要完成后,我想制作一个很棒的向导,让你一路走来。



所以我有一个基本的模板,它会有步骤,有时不需要创建一个新的数据,如果存在,所有就绪。
所以我已经在模板上显示了每个表格,所以用户可以检查表中是否包含他需要下一步或他需要创建的表。



我做了一个cbv FormView
,我试图从GET获取一步,并加载所需的ModelForm



model.py

  A类(models.Model):
user = CharField()
class B(models.Model):
pupy = CahrField()

form.py

  AForm(forms.ModelForm):
class Meta:
model = A
fields = ['user']

BForm(forms.ModelForm):
class Meta:
model = B
fields = ['pupy']

views.py

 类向导(FormView)
template_name = 'test.html'
def get(self,request,* args,** kwargs):
self.step = self.request.GET.get('step')
self。 form_class = FORM [self.step]#F ORM = dict {'step':ModelForm}
def form_valid(self,form):
self.object = form.save(commit = False)
self.object.save()

我无法在这里输入所有已经尝试的一切,基本上我
确实得到我的通过叮叮叮叮叮叮ing cl to h h h uble uble uble uble uble uble uble uble uble uble,,,,,,if if if if if>>>>>>>>>>>>>> pre> def __init__
self.step ='first_step'

只是作为一个测试

解决方案

发现另一个帖子的答案是通过组合2个人回答
form_class和template_name的切换案例



@Denny Crane

  class Foo(FormView):
def dispatch ,request,* args,** kwargs):
self.var = request.session ['sessi onvar'] ['var']
如果self.var == some_value:
form_class = form1
template_name = template1
elif self.var == another_value:
form_class = form2
template_name = template2
[...]
return super(Foo,self).dispatch(request,* args,** kwargs)

@Serafeim



需要重写get_form_class()以及

  def get_form_class(s​​elf):
self.form_class = FORM [self.step]
return self.form_class


I am trying to build a Wizard for populating data in to DataBase. I try the session.Wizard with out success plus I don't think this is what ti need. correct me if I am wrong.

so basically my database have some tables which needs to be fill, because of the joints need to be done, I wanted to make a nice wizard which takes you along the way.

so I have a base template which would have steps, sometimes there would be no need in creating a new data if exists all ready. so I have made a display of each table on template so user can check if the table contain what he needs for next step or he needs to create it.

I made a cbv FormView and I am trying to get the step from GET and load the needed ModelForm

model.py

Class A(models.Model):
    user = CharField()
class B(models.Model):
    pupy = CahrField()

form.py

AForm(forms.ModelForm):
    class Meta:
        model=A
        fields = ['user']

BForm(forms.ModelForm):
    class Meta:
        model=B
        fields = ['pupy']

views.py

class Wizard(FormView)
    template_name = 'test.html'
    def get(self, request, *args, **kwargs):
        self.step = self.request.GET.get('step')
        self.form_class = FORM[self.step] # FORM = dict {'step': ModelForm}
    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.save()

I can't type here everything I have tried so far, basically I do get my form in to the html by clinking the links but I am having a truble saving them, I have manage to save the first form if I I do

def __init__
    self.step = 'first_step'

just as a test

解决方案

found an answer from another post that is working by combining 2 people answer Switch case for form_class and template_name

@Denny Crane

class Foo(FormView):
    def dispatch(self, request, *args, **kwargs):
        self.var = request.session['sessionvar']['var']
        if self.var == some_value:
            form_class = form1
            template_name = template1
        elif self.var == another_value:
            form_class = form2
            template_name = template2
        [...]
        return super(Foo, self).dispatch(request, *args, **kwargs)

@Serafeim

need to override get_form_class() as well

def get_form_class(self):
    self.form_class = FORM[self.step]
    return self.form_class

这篇关于FormView CBV根据请求更改ModelForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆