且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django 1.2.1许多字段的内联管理

更新时间:2023-01-11 12:33:14

尝试做?

a:

# Models:

class Ingredient(models.Model):
    name = models.CharField(max_length=128)

class Recipe(models.Model):
    name = models.CharField(max_length=128)
    ingredients = models.ManyToManyField(Ingredient, through='RecipeIngredient')

class RecipeIngredient(models.Model):
    recipe = models.ForeignKey(Recipe)
    ingredient = models.ForeignKey(Ingredient)
    amount = models.CharField(max_length=128)


# Admin:

class RecipeIngredientInline(admin.TabularInline):
    model = Recipe.ingredients.through

class RecipeAdmin(admin.ModelAdmin):
    inlines = [RecipeIngredientInline,]

class IngredientAdmin(admin.ModelAdmin):
    pass

admin.site.register(Recipe,RecipeAdmin)
admin.site.register(Ingredient, IngredientAdmin)

b:

# Models:

class Recipe(models.Model):
    name = models.CharField(max_length=128)

class Ingredient(models.Model):
    name = models.CharField(max_length=128)
    recipe = models.ForeignKey(Recipe)


# Admin:

class IngredientInline(admin.TabularInline):
    model = Ingredient

class RecipeAdmin(admin.ModelAdmin):
    inlines = [IngredientInline,]

admin.site.register(Recipe,RecipeAdmin)