代码之家  ›  专栏  ›  技术社区  ›  keisaac

在通用视图上使用中间表格行应用Django admin m2m

  •  0
  • keisaac  · 技术社区  · 6 年前

    我已经完成了我想要的,但是在Admin上我的Admin.py使用 admin.TabularInline

    class RatioInline(admin.TabularInline):
        model = Ratio
    
    class MenuAdmin(admin.ModelAdmin):
        inlines = (RatioInline,)
    

    我需要的是使这个添加行管理表单,以放在我的视图。 是否可以在基于类的管理视图中实现此表单?

    enter image description here

    class Menu(models.Model):
        name = models.CharField(max_length=24)
        ratio = models.ManyToManyField('Ingredient', through='Ratio')
    
    class Ratio(models.Model):
        menu = models.ForeignKey('Menu', on_delete=models.CASCADE)
        ingredient = models.ForeignKey('Ingredient', on_delete=models.CASCADE)
        quantity = models.DecimalField(max_digits=12, decimal_places=2)
    
    class Ingredient(models.Model):
        name = models.CharField(max_length=24)
    
    0 回复  |  直到 6 年前