来自forms.Form的访问请求从dB获取与用户相关的数据

时间:2017-05-13 08:23:10

标签: python django forms django-forms django-request

所以基本任务是:使用链接到经过身份验证的用户的dB数据,将模板名称和文本添加到choices ChoiceField窗口小部件的Selector中。 Template是与用户关联的模型ForeignKey

我想通过链接到连接视图的Form类作为django.views.generic.View类来访问请求数据(用户)。

我在这里检查了类似的问题: Curious about get_form_kwargs in FormView

在这里:Sending request.user object to ModelForm from class based generic view in Django

在这里:Django: Accessing request in forms.py clean function

然而,他们不会触及非FormView类的基础。而且由于它是一个相当古老的解决方案,我很好奇是否有更方便的方法从request课程到达forms.Form

这是我的代码:

views.py

class InformFill(View):
form_class = InformForm
temlate_name = 'distrib_db/inform_fill.html'

def get(self, request):
    if request.user.is_authenticated():
        form = self.form_class(None)
        return render(request, self.temlate_name, context={'form': form})
    else:
        return redirect('distrib_db:login')

def post(self, request):
    if request.user.is_authenticated():
        form = self.form_class(request.POST)
        if form.is_valid():
            inform = Inform(flt_numbr=form.cleaned_data['flight_number'], date=form.cleaned_data['date'],
                            template=form.cleaned_data['text'], user=request.user)
            inform.save()
            date = form.cleaned_data['date']
            flt_numbr = form.cleaned_data['flight_number']
            try:
                emails, contacts = get_mail_cnt(date, flt_numbr)
                # inform = get_object_or_404(Inform, pk=request['pk'])
                paxdata = PaxData(inform=inform, emails=' '.join(emails), contacts=' '.join(contacts))
                paxdata.save()
                return redirect('/inform/{0}/'.format(inform.pk))
            # 'distrib_db:detail', context={'pk': inform.id}
            except Exception as e:
                return render(request, 'distrib_db/sample.html',
                              context={'date': date, 'flight_number': flt_numbr, 'error': e})

                # return render(request, 'distrib_db/sample.html', context={'date': date, 'flt_numbr': flt_numbr})

        return render(request, self.temlate_name, context={'form': form})
    else:
        return redirect('distrib_db:login')

forms.py

class InformForm(forms.Form):
flight_number = forms.CharField(5, widget=forms.TextInput())
date = forms.DateField(widget=forms.DateInput(attrs={'class': 'datepicker'}))
template = forms.ChoiceField(choices=templates, widget=forms.Select(attrs={'id': 'select_box',
                                                                          'onchange': 'javascript:changer();'}))
text = forms.CharField(widget=forms.Textarea(attrs={'id': 'txt_box', 'class': 'latin',
                                                    'maxlength': "160", 'onchange': 'javascript:validateTextArea();'}))

一般来说,我想像这样实现smth:

`class InformForm(forms.Form):
    def get_template_choices(self):
        templates = self.request.user.template_set.all()
        choices = []
        for t in templates: 
            choices.append((t.text, t.name))
        return choices

    flight_number = forms.CharField(5, widget=forms.TextInput())
    date = forms.DateField(widget=forms.DateInput(attrs={'class':                                                    
                                                      'datepicker'}))
    template = forms.ChoiceField(choices=get_template_choices(), 
                                         widget=forms.Select(attrs=
                                        {'id': 'select_box',

                                         'onchange': 'javascript:changer();'}))
    text = forms.CharField(widget=forms.Textarea(attrs={'id': 'txt_box', 
                                                    'class': 'latin',
                                                    'maxlength': "160", 
                              'onchange': 'javascript:validateTextArea();'}))`

我很感激任何方法,我缺乏知识并且问新手问题,对不起。

我只想获得python-way解决方案而不是构建一些js/jinja遍历。

感谢您的时间!

在@Danielius评论之后,我做了一些调整:

`class InformForm(forms.Form):
    def __init__(self, user=None, *args, **kwargs):
        if user:
            self.fields['template'] = forms.ChoiceField(choices=tuple([(template.text, template.name) for template in user.template_set.all()]),
                                                        widget=forms.Select(attrs={'id': 'select_box', 'onchange': 'javascript:changer();'}))

    flight_number = forms.CharField(5, widget=forms.TextInput())
    date = forms.DateField(widget=forms.DateInput(attrs={'class': 'datepicker'}))
    # template = forms.ChoiceField(choices=templates, widget=forms.Select(attrs={'id': 'select_box',
    #                                                                           'onchange': 'javascript:changer();'}))
    text = forms.CharField(widget=forms.Textarea(attrs={'id': 'txt_box', 'class': 'latin',
                                                        'maxlength': "160", 'onchange': 'javascript:validateTextArea();'}))`

收到错误AttributeError: 'InformForm' object has no attribute 'fields'

1 个答案:

答案 0 :(得分:1)

您可以通过更改__init__方法将请求传递到您的表单:

class InformForm(forms.Form):
...
   def __init__(self, user=None,*args, **kwargs):
        super(InformForm, self).__init__(**kwargs)
        if user:  
            self.fields['somefield'] = forms.ChoiceField()                  
            self.fields['somefield'].widget = forms.Select()
            self.fields['somefield'].queryset = Someobject.objects.filter(User=user)
...

如果用户通过外键链接到db中的其他对象,那么您将获得其他对象的所有值作为选择项。

此外,在创建表单时,您可以像这样传递用户:

form= InformForm(user=request.user,data=request.POST)