Python Django:父对象没有属性子对象集
我有这两种型号:Python Django:父对象没有属性子对象集,python,django,model,Python,Django,Model,我有这两种型号: class Periodo(models.Model): #国际开发署usato根据国际医疗保健文件 #法图拉的莱维托周期 data_i_p=models.DateField('data IO',blank=True) data\u f\u p=models.DateField('data fine',blank=True) mc_p=models.DecimalField('mc',max_digits=7,decimal_places=0,blank=True,defaul
class Periodo(models.Model):
#国际开发署usato根据国际医疗保健文件
#法图拉的莱维托周期
data_i_p=models.DateField('data IO',blank=True)
data\u f\u p=models.DateField('data fine',blank=True)
mc_p=models.DecimalField('mc',max_digits=7,decimal_places=0,blank=True,default=0)
IDbatt=models.ForeignKey(FattAqp,verbose_name=“fatturaqp”,on_delete=models.CASCADE,related_name='periodo_rel')
descr=models.CharField('descrizione',最大长度=50)
objects=models.Manager()
类元:
详细名称='Periodo'
class DettFAqp(models.Model):
INPONIBILE=models.DecimalField('INPONIBILE',最大位数=8,小数位数=2,默认值=0)
iva=models.DecimalField(“%iva”,最大位数=4,小数位数=2,默认值=10)
mc=models.DecimalField('qtá(gg/mc'),最大位数=7,小数位数=0,空格=True,默认值=0)
voce=models.ForeignKey(VoceAqp,verbose_name=“metodo di ripart.”,on_delete=models.CASCADE)
periodo=models.ForeignKey(periodo,verbose_name=“fattura中的periodo”,在_delete=models.CASCADE上,
相关的\u name='dettfaqp',帮助\u text=u“periodo cui la voce公寓”)
rigo=models.DecimalField('rigo',max\u digits=2,decimal\u places=0,default=0,help\u text=u“rigo di fattura”)
当我尝试访问与父对象相关的集合时,我会出现以下错误:
>>> p=Periodo.objects.get(pk=2)
>>> p
<Periodo: consumo accertato in 344 gg>
>>> p.dettfaqp_set.all()
Traceback (most recent call last):
File "<input>", line 1, in <module>
p.dettfaqp_set.all()
AttributeError: 'Periodo' object has no attribute 'dettfaqp_set'
>>>p=Periodo.objects.get(pk=2)
>>>p
>>>p.dettfaqp_set.all()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
p、 dettfaqp_set.all()
AttributeError:“Periodo”对象没有属性“dettfaqp\u集”
有什么建议吗?如果您使用的是
相关的\u name
属性,则不需要在调用结束时包含\u set:只需使用以下内容:
p.dettfaqp.all()
它应该会起作用
如果您尚未为外键
指定相关的\u名称
,则仅使用\u集,在您的示例中,它将是:
p.periodo_set.all()
您已将
DettFAqp.periodo
的相关\u名称设置为“DettFAqp”
。您应该使用该属性p.dettfaqp.all()
您指定了一个:
class DettFAqp(models.Model):
# …
periodo = models.ForeignKey(
Periodo,
verbose_name='Periodo in fattura',
on_delete=models.CASCADE,
related_name='dettfaqp',
help_text='periodo cui la voce appartiene'
)
p.dettfaqp.all()