'QueryDict'对象没有属性'user'

时间:2020-03-16 08:12:00

标签: python django

我目前正在Django中从事我的项目,我需要将外键(名称)分别显示给当前用户。就像我想要的那样,它显示得非常好,但是在我提交表单时,“ QueryDict”对象没有属性“ user”时会引发错误。

错误消息

Traceback (most recent call last):
File "C:\Users\KARTHICK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\KARTHICK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\KARTHICK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\KARTHICK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "F:\TECH\emp\crm\employee\views.py", line 26, in employee_form
form = EmployeeForm(request.POST)
File "F:\TECH\emp\crm\employee\forms.py", line 40, in __init__
self.fields['designation'].queryset = req.user.designation.all()
AttributeError: 'QueryDict' object has no attribute 'user'
[16/Mar/2020 13:06:45] "POST /employee/add/ HTTP/1.1" 500 78894

models.py

class Designation(models.Model):
     user = models.ForeignKey(User,on_delete=models.CASCADE,related_name="designation",null=True,blank=True)
     title = models.CharField(max_length=100)
class Employee(models.Model):
     user = models.ForeignKey(User,on_delete=models.CASCADE,related_name="employee",null=True,blank=True)                                                           
     designation = models.ForeignKey(Designation,on_delete=models.CASCADE)

forms.py

def __init__(self,req,*args,**kwargs):
    super(EmployeeForm,self).__init__(*args,**kwargs)
    self.fields['designation'].queryset = req.user.designation.all()

views.py

 def employee_form(request,id=0):
  if request.method == 'GET':
    if id == 0:       
        req = request.user.designation.all()     
        form = EmployeeForm(req)
    else:

        employee = Employee.objects.get(pk=id)
        if employee in request.user.employee.all():
            form = EmployeeForm(request,instance=employee)
        else:
            return redirect('/employee')
    return render(request,'employee-form.html',{'form':form})
else:
    if id==0:
        form = EmployeeForm(request.POST)
    else:

        employee = Employee.objects.get(pk=id)
        if employee in request.user.employee.all():
            form = EmployeeForm(request.POST,instance=employee)
    if form.is_valid():
        n = form.cleaned_data["name"]
        t = Employee(name=n)
        t.save()
        request.user.employee.add(t)

    return redirect('/employee')

3 个答案:

答案 0 :(得分:0)

在views.py

def employee_form(request,id=0):
  if request.method == 'GET':
    if id == 0:       
        req = request.user.designation.all() //but here you are passing queryset    
        form = EmployeeForm(req)

因此,使用form = EmployeeForm(req)form = EmployeeForm(request)代替form = EmployeeForm(request.user)

答案 1 :(得分:0)

更改

form = EmployeeForm(req)

form = EmployeeForm(request)

您的Employee表单需要一个请求对象作为参数,但是您向其传递了Designations的查询集。

答案 2 :(得分:0)

非常感谢您的回复。我设法消除了错误。我没有在POST方法

中传递请求参数

views.py

form = EmployeeForm(request,request.POST)