Django:'NoneType'对象没有属性'user'

时间:2016-11-23 05:37:27

标签: django django-forms django-views

我在其中一个选择字段中使用Django User模型。 但是,我无法仅传递链接到commmon组织/公司的用户。

我为每个用户链接了一个onetoone个人资料。这个对于onetoone模型再次与组织模型相关联。

现在需要一个表单,其中select字段应该将用户链接到组织模型。

这是我的form.py:

class taskaddform(forms.ModelForm): 
    def __init__(self,user, *args, **kwargs):
        self.user = kwargs.pop('user', None)
        self.request = kwargs.pop('request', None)
        super(taskaddform, self).__init__(*args, **kwargs)
        self.fields["assigned_to"].queryset=User.objects.filter(company=self.request.user.company.entity)   
    class Meta:
        model=tasktable
        fields=('task','assigned_to','discription','documents',)

views.py:

@login_required
def taskadd(request):
    if request.method=='POST':
        form=taskaddform(request.POST,request.FILES,request.user)
        if form.is_valid():
            new_form=form.save(commit=False)
            new_form.created_by=request.user
            new_form.company=request.user.company.entity
            new_form.save()
            return HttpResponseRedirect(reverse('taskadd'),messages.add_message(request, messages.SUCCESS,'Task added succesfully.'))
    else:
            form=taskaddform(request.user) 

    return render(request,'taskadd.html',{'form':form})

错误日志:

Request Method:     GET
Request URL:    http://127.0.0.1:8000/task/taskadd/
Django Version:     1.10
Exception Type:     AttributeError
Exception Value:    

'NoneType' object has no attribute 'user'

回溯:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/task/taskadd/

Django Version: 1.10
Python Version: 2.7.10
Installed Applications:
('invoice',
 'support',
 'task',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'widget_tweaks')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')



Traceback:

File "/Library/Python/2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/Users/pulkitsharma/Documents/workspace1/tiktant/task/views.py" in taskadd
  38.             form=taskaddform(request.user) 

File "/Users/pulkitsharma/Documents/workspace1/tiktant/task/forms.py" in __init__
  11.         self.fields["assigned_to"].queryset=User.objects.filter(company=self.request.user.company.entity)   

Exception Type: AttributeError at /task/taskadd/
Exception Value: 'NoneType' object has no attribute 'user'

1 个答案:

答案 0 :(得分:1)

更新了views.py。

@login_required
def taskadd(request):
    if request.method=='POST':
        form=taskaddform(request, request.POST,request.FILES,request.user)
        if form.is_valid():
            new_form=form.save(commit=False)
            new_form.created_by=request.user
            new_form.company=request.user.company.entity
            new_form.save()
            return HttpResponseRedirect(reverse('taskadd'),messages.add_message(request, messages.SUCCESS,'Task added succesfully.'))
    else:
            form=taskaddform(request) 

    return render(request,'taskadd.html',{'form':form})

并在您的forms.py -

class taskaddform(forms.ModelForm): 
    def __init__(self, request, *args, **kwargs):
        self.request = request
        super(taskaddform, self).__init__(request, *args, **kwargs)
        self.fields["assigned_to"].queryset=User.objects.filter(company=self.request.user.company.entity)