Django: 'NoneType' object has no attribute 'user'
<p>I am using Django User model for one of the select field.
However i am not able to pass only users linked to a commmon organisation/company.</p>

<p>I have a onetoone profile linking for each user. This one to onetoone model is again linked to organisation model.</p>

<p>Now need a form where select field should have users linked to organisation model.</p>

<p>Here is my form.py:</p>

<pre><code>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',)
</code></pre>

<p>views.py:</p>

<pre><code>@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})
</code></pre>

<p>Error Log:</p>

<pre><code>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'
</code></pre>

<p>traceback:</p>

<pre><code>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'
</code></pre>