我已经完成了我想要的,但是在Admin上我的Admin.py使用
admin.TabularInline
class RatioInline(admin.TabularInline):
model = Ratio
class MenuAdmin(admin.ModelAdmin):
inlines = (RatioInline,)
我需要的是使这个添加行管理表单,以放在我的视图。
是否可以在基于类的管理视图中实现此表单?
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)