[Fixed]-Django: 'NoneType' object has no attribute 'user'

1👍

Updated 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})

and in your 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)

Leave a comment