我知道要選擇一個相關的對象應該做object.related_set.all()
。然而在我的情況下,這產生了一個AttributeError,所以很難調試。下面是設置:在Django中通過外鍵選擇相關字段
class Action(models.Model):
...
class FieldDiff(models.Model):
action = models.ForeignKey(Action, editable=False, related_name='Action')
,並且在Shell:
In [16]: act = Action.objects.get(pk=34)
In [17]: act.fielddiff_set.all()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/home/ipantuyev/progs/ccc_app/<ipython console> in <module>()
AttributeError: 'Action' object has no attribute 'fielddiff_set'
我想這與從駝峯翻譯做。我可以解決這個問題,但是這個代碼會從模板中調用,所以我寧願沒有額外的邏輯。
謝謝你們。應該抓住那個。 – 2012-01-30 22:17:49