QuerySet, Object has no attribute id - Django

前端 未结 4 667
死守一世寂寞
死守一世寂寞 2021-02-01 04:24

I\'m trying to fetch the id of certain object in django but I keep getting the following error Exception Value: QuerySet; Object has no attribute id. my function in views.py

4条回答
  •  礼貌的吻别
    2021-02-01 05:03

    I got this error for almost 2 days, the main issue for this error solely depends on two files i.e.

    models.py & views.py

    I was getting this error because I wanted to create session from email id but it shows their is no attribute email so it wasn't fetching any str object.

    Solution:-

    models.py

    class Register(models.Model):
    userid = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100)
    email = models.EmailField(max_length=200)
    password = models.CharField(max_length=100)
    
    def __str__(self):
        return "%s %s" %(self.name, self.email)
    

    Create a string for the following you want data from according to your project.

    views.py

        if request.method == "POST":
            emailx1 = request.POST['emailx']
            passwordx1 = request.POST['passwordx']
            if (Register.objects.filter(email=emailx1, password=passwordx1)).exists():
                a = Register.objects.filter(email=emailx1).first()
                request.session['session_name'] = a.email
                request.session['session_id'] = a.userid
                return render(request, "index.html", {"a": a})
    

    Use .first() method with your Model.objects method. This have resolved my problem hope it would resolves yours too.

提交回复
热议问题