Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - ReverseManyToOneDescriptor' object has no attribute 'all'

Tags:

django

I can see alot of related questions to mine and when i check them out, from what i can see my code should be working, but im getting the below error?

models:

class Circuits(models.Model):    
    site_data = models.ForeignKey(SiteData,verbose_name="Site")   
    order_no = models.CharField(max_length=200,verbose_name="Order No")
    expected_install_date = models.DateField()
    install_date = models.DateField(blank=True,null=True)
    circuit_type = models.CharField(max_length=100,choices=settings.CIRCUIT_CHOICES)    
    circuit_preference = models.CharField(max_length=20,verbose_name="Circuit Preference",choices=settings.CIRCUIT_PREFERENCE,blank=True,null=True)
    circuit_speed = models.IntegerField(blank=True,null=True)
    circuit_bearer = models.IntegerField(blank=True,null=True)

class CircuitMaintenance(models.Model): 
    circuit = models.ForeignKey(Circuits,verbose_name="Circuit", related_name='maintenance')
    ref = models.CharField(max_length=200,verbose_name="Ref No")
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    notes = models.TextField(blank=True,null=True)

error

circuits = Circuits.objects.filter(site_data__id=1)
for i in Circuits.maintenance.all():
 print i
AttributeError: 'ReverseManyToOneDescriptor' object has no attribute 'all'

EDIT: amended with lower case c still getting error:

>>> from sites.models import Circuits
>>> circuits = Circuits.objects.filter(site_data__id=1)
>>> for i in circuits.maintenance.all():
...  print i
...
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'QuerySet' object has no attribute 'maintenance'
>>>
like image 916
AlexW Avatar asked Dec 30 '16 15:12

AlexW


1 Answers

You need to used your variable circuits

circuits = Circuits.objects.filter(site_data__id=1)
for cm in circuits:
    maintenances = cm.maintenance.all()
    for maintenance in maintenances:
         print(maintenance )

If you don't define a related_name, you can do:

circuits.circuitmaintenance_set.all() :
like image 118
Wilfried Avatar answered Oct 07 '22 12:10

Wilfried