我正在尝试在表单类中访问已验证的用户。我尝试通过从视图传递请求对象到类初始化来实现此功能,但似乎有些不规范。是否有更好的方法可以在视图之外访问已验证的用户或请求对象?
class LicenseForm(forms.Form):
def __init__(self, *args, **kwargs):
#self.fields[ license ] = forms.ModelChoiceField(queryset=self.license_queryset(), empty_label= None , widget=forms.RadioSelect())
def license_queryset():
queryset = License.objects.filter(organization__isnull=True)
# add addtional filters if the logged in user belongs to an organization
return queryset
licenses = forms.ModelChoiceField(queryset=license_queryset(), empty_label= None , widget=forms.RadioSelect())