I have two models called car and producer. The two models have many to one relation between them.
class Producer(models.Model):
name = models.CharField(max_length=30)
def __str__(self):
return self.name
class Car(models.Model):
name = models.CharField(max_length=20)
producer = models.ForeignKey(Producer, on_delete=models.CASCADE)
def __str__(self):
return self.name here
When i try to query the reverse relationship.
Producer.objects.filter(car__name='Mini')
then it return a queryset object
<QuerySet [<Producer: BMW>]>
when i try to assign the queryset object to a variable and fetch the name field result then it gives error.
obj1 = Producer.objects.filter(car__name='Mini')
In [6]: obj1.name
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-6-5155cb2773b4> in <module>
----> 1 obj1.name
AttributeError: 'QuerySet' object has no attribute 'name'
what could be the reason for this error