如何访问Django中forms.py中的request.session.get[]变量值

of1yzvn4  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(86)

*我想使用forms.py中的request.session.get[] model choice字段填充 form.py文件

class AssetTagForm(ModelForm):
    assets = forms.ModelChoiceField(widget=forms.Select(attrs={'class': 'form-control'}) ,queryset=Asset.objects.filter(branch = self.session['branch']))
    class Meta:
        model = AssetTag
        fields = '__all__'
        labels = {
            'tag':'Asset Tag',
        }
        exclude = ['created_by',]
        widgets = {
            'branch':forms.CheckboxSelectMultiple(attrs={}),
            'code':forms.TextInput(attrs={'class':'form-control', 'placeholder':'Code','readonly':'readonly'}),
            'tag':forms.TextInput(attrs={'class':'form-control', 'placeholder':'Asset Tag'}),
            'status':forms.Select(attrs={'class':'form-control'}),
        }

字符串

我不知道form.py文件中request.session.get[]变量的用法

hm2xizp9

hm2xizp91#

您可以在初始化表单时填充初始数据。
参考:https://docs.djangoproject.com/en/4.2/ref/forms/api/

>>> initial = {"key": "value"}
>>> unbound_form = AssetTagForm(initial=initial)

字符串

相关问题