2016-11-22 109 views
0

我希望能夠創建一個模型TestCase,其中有一個products字段,該字段是另一個外鍵,用於禁止模型Product。我希望能夠使用管理頁面創建新的Product,然後創建TestCase我希望能夠從Product中選擇我已經創建的。一個保管箱將是理想的。這是我曾嘗試:在管理頁面上添加Django ForeignKey作爲下拉選擇

models.py

class TestCase(TestBase): 

    def __str__(self): 
     return self.title 


class Product(models.Model): 
    products = models.ForeignKey(TestCase, on_delete=models.CASCADE, null=True) 
    name = models.CharField(max_length=200, default='') 

    def __str__(self): 
     return self.name 

admin.py

class ProductInline(admin.TabularInline): 

    model = Product 


class TestCaseAdmin(admin.ModelAdmin): 

    inlines = [ProductInline] 

admin.site.register(TestCase, TestCaseAdmin) 

管理頁面現在有字段來創建一個產品上添加新TestCase,但我想改爲從已創建的Products中選擇?

在此先感謝。

+0

產品是否屬於多個TestCase? –

+0

@DanielRoseman是的。 – EngineerCamp

回答

2

您有錯誤的模型結構。如果您希望能夠從您的TestCase中的已有產品列表中進行選擇,並且產品可以屬於多個TestCases,則您需要一個位於TestCase上的ManyToManyField。