2
我試圖做這些3個表使用PersonScore的內部聯接,但它無法找到persontype。我究竟做錯了什麼?對prefetch_related無效參數()
型號:
class PersonScore(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
person = models.ForeignKey(‘Person’)
class Person(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
name = models.CharField(max_length=255)
class PersonType(models.Model):
person = models.ForeignKey(‘Person’)
type = models.CharField(max_length=255)
代碼:
PersonScore.objects.filter(person__name="Bob").prefetch_related("person__persontype")
錯誤:
Cannot find 'persontype' on Person object, 'person__persontype' is an invalid parameter to prefetch_related()
代碼:
PersonScore.objects.filter(person__name="Bob").prefetch_related("person__persontype_set")
錯誤:
KeyError: (1L,)
還有問題嗎?或解決? –