2011-12-01 51 views
0

因此,這是元meta中的一個小練習。我希望能夠將模型引用存儲爲與另一個模型關聯的表中的一行。事情是這樣的:如何在另一個模型實例中存儲對模型類型的引用

class Widget(models.Model): 
    related = models.Model() # data model associated with this widget 
    identifier = models.CharField(max_length=500) # human-friendly descriptor 

這不驗證..我發現一個acceptable workaround,但我不知道是否有這樣做的更合適的/優雅的方式。

謝謝,django嚮導!

回答

2

如果我正確理解你的問題,那麼你需要GenericForeignKey。你看過嗎?

+0

我是這麼認爲的。說實話,在這裏閱讀答案和相關的文件讓我回到繪圖板 - 但這是一件好事,對吧? 謝謝! – btk

0

如果你只想抱着另一個對象的實際模型,你可以簡單地用一個ForeignKey到content type

from django.contrib.contenttypes.models import ContentType 
class Widget(models.Model): 
    related = models.ForeignKey(ContentType) 
    identifier = models.CharField(max_length=500) # human-friendly descriptor 
相關問題