我有以下型号:
models.py
class Invoice(models.Model):
# invoice fields
class LineItem(models.Model):
invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE, related_name='lineitems')
code = models.ForeignKey(Code, on_delete=models.CASCADE, related_name='lineitems')
# other fields
我相信我应该能够通过invoice.lineitems.all()
访问发票的相关对象,并通过code.lineitems.all()
访问代码,其中invoice
和code
是模型类的实例。< / p>
但是,如果我这样做:
x = Invoice.objects.last()
x.lineitems.all()
我收到以下错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "../.venv/lib/python2.7/site-packages/django/db/models/fields/related.py", line 816, in __get__
return self.related_manager_cls(instance)
File "../.venv/lib/python2.7/site-packages/django/db/models/fields/related.py", line 683, in __init__
super(RelatedManager, self).__init__()
TypeError: __init__() takes exactly 2 arguments (1 given)
我是否错误地访问了这个?也许某个地方有名字冲突?