2016-11-06 77 views
0

我想讓一個TabularInline與多對多關係一起工作,但我只能得到要顯示的關係對象。如果我使用外鍵關係,它工作正常。如何使多對多的關係像外鍵關係一樣行事?

所以我們可以說這是我的許多一對多的關係:

# Jobs have many workers, and workers can be assigned to many jobs 

class Job(models.Model): 
    workers = models.ManyToManyField(Worker, related_name='jobs') 

class Worker(models.Model): 
    name = models.CharField(max_length=255) 

    # workers relationships with other models 
    insurance = models.ForeignKey(Insurance, null=True, blank=True, default=None) 
    location = models.ForeignKey(Location, null=True, blank=True, default=None) 

這並不與TabularInline很好地發揮它只會顯示Job_Worker對象。

所以我想知道有什麼方法可以使關係看起來像一個外鍵關係?例如,worker獲得一箇中間表的外鍵,並使用「through」。對於TabularInline,最終結果是「開箱即用」。

謝謝。

回答

1

恐怕你不能用股票的Django工具來做。這是很好的。它可以防止您修改多對多另一端的相關對象,而這些對象實際上可能被其他實例使用。

如果您感到絕望並且必須完全按照您所描述的那樣進行操作,那麼您唯一的方法就是爲您的內聯管理員創建自定義表單。也是described in the documentation