gpt4 book ai didi

Django - ReverseManyToOneDescriptor' 对象没有属性 'all'

转载 作者:行者123 更新时间:2023-12-04 23:39:44 32 4
gpt4 key购买 nike

我可以看到很多与我相关的问题,当我检查它们时,我可以看到我的代码应该可以工作,但是我收到以下错误?

楷模:

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)

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

编辑:
修改为小写 c 仍然出现错误:
>>> 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'
>>>

最佳答案

您需要使用您的变量 circuits

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

如果您没有定义 related_name , 你可以做:
circuits.circuitmaintenance_set.all() :

关于Django - ReverseManyToOneDescriptor' 对象没有属性 'all',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41398415/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com