0

How to solve : AttributeError: 'QuerySet' object has no attribute 'name'

models.py

from django.db import models


class Real(models.Model):
    name = models.CharField(max_length=200, default='')
    f_name = models.CharField(max_length=300, default='')

    def __str__(self):
        return self.name


>>> from real.models import Real
>>> list = Real.objects.all()
>>> list
<QuerySet [<Real: Umair Khan>, <Real: Uzair Khan>, <Real: Anas>]>
>>> list.name
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'QuerySet' object has no attribute 'name'
>>>

1 Answer 1

1

Referencing the field in the model is not possible for Queryset, but for Model instance. So try this:

    for real in list:
        print(real.name)

Or if you want the list of names, then try this:

    [real for real in list.values('name')]

This will return the list of dictionary i.e.

    [ {'name': name}, ...]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.