Django之全局使用request.user.username示例代码

def pro_mgr():
    """
    获取当前登陆用户的项目
    :return:
    """
    if DEVELOP_USER_NAME == 'admin':
        return Project.objects.all()
    return Project.objects.filter(prj_mgr=DEVELOP_USER_NAME)


class DeployApplyForm(ModelForm):
    # project = forms.ModelChoiceField(queryset=pro_mgr())
    bgn_time = forms.DateTimeField(initial=datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), label=u'开始',
                                   input_formats=['%Y-%m-%dT%H:%M:%S'], widget=forms.DateTimeInput(
            attrs={'type': 'datetime-local', 'emptyTips': u'请选择时间', 'step': '1'}))
    end_time = forms.DateTimeField(initial=datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), label=u'结束',
                                   input_formats=['%Y-%m-%dT%H:%M:%S'], widget=forms.DateTimeInput(
            attrs={'type': 'datetime-local', 'emptyTips': u'请选择时间', 'step': '1'}))

    class Meta:
        model = DeployApply
        fields = '__all__'


    def __init__(self, *args, **kwargs):
        prj_mgr = kwargs.pop('prj_mgr', '')
        super(DeployApplyForm, self).__init__(*args, **kwargs)
        if prj_mgr == 'admin':
            self.fields['project'] = forms.ModelChoiceField(queryset=Project.objects.all())
        else:
            self.fields['project'] = forms.ModelChoiceField(queryset=Project.objects.filter(prj_mgr=prj_mgr))
//views.py
deployApplyForm = DeployApplyForm(initial=wfContent[0], prefix='deployApply',prj_mgr=request.user.username)
//settings.py
# DEVELOP模式下登陆用户
DEVELOP_USER_NAME = 'admin'

你可能感兴趣的:(django,web框架)