从多对多字段中选择项目到列表中的 link 个特定项目到管理面板中的模型
Selecting items from a many to many field to link specific items in the list to a model in the admin panel
我正在开发食谱书应用程序,目前我的模型以这种方式连接:
class Tool(models.Model):
name = models.CharField(max_length=50)
description = models.CharField(max_length=200)
def __str__(self):
return self.name
class Recipe(models.Model):
name = models.CharField(max_length=50)
description = models.CharField(max_length=200)
servings = models.IntegerField(default=1, blank=False)
tools = models.ManyToManyField(Tool)
def __str__(self):
return self.name
管理面板输入当前如下所示:
如截图所示保存数据后,出现以下错误:
OperationalError at /admin/recipeBook/recipe/add/
no such table: recipeBook_recipe_tools
这是我的admin.py,以防有用:
from django.contrib import admin
from .models import Recipe, Ingredient, Instruction, Tool
# Register your models here.
class IngredientInline(admin.TabularInline):
model = Ingredient
extra = 2
class RecipeAdmin(admin.ModelAdmin):
fieldsets = [
('Information', {'fields': ['name', 'description', 'servings']}),
('Tools', {'fields': ['tools']})
]
inlines = [IngredientInline]
list_display = ('name', 'description', 'servings')
search_fields = ['name']
list_filter = ['servings']
admin.site.register(Recipe, RecipeAdmin)
admin.site.register(Ingredient)
admin.site.register(Instruction)
admin.site.register(Tool)
如果你遇到这样的错误。
确保在添加到模型后迁移数据库。
我正在开发食谱书应用程序,目前我的模型以这种方式连接:
class Tool(models.Model):
name = models.CharField(max_length=50)
description = models.CharField(max_length=200)
def __str__(self):
return self.name
class Recipe(models.Model):
name = models.CharField(max_length=50)
description = models.CharField(max_length=200)
servings = models.IntegerField(default=1, blank=False)
tools = models.ManyToManyField(Tool)
def __str__(self):
return self.name
管理面板输入当前如下所示:
如截图所示保存数据后,出现以下错误:
OperationalError at /admin/recipeBook/recipe/add/
no such table: recipeBook_recipe_tools
这是我的admin.py,以防有用:
from django.contrib import admin
from .models import Recipe, Ingredient, Instruction, Tool
# Register your models here.
class IngredientInline(admin.TabularInline):
model = Ingredient
extra = 2
class RecipeAdmin(admin.ModelAdmin):
fieldsets = [
('Information', {'fields': ['name', 'description', 'servings']}),
('Tools', {'fields': ['tools']})
]
inlines = [IngredientInline]
list_display = ('name', 'description', 'servings')
search_fields = ['name']
list_filter = ['servings']
admin.site.register(Recipe, RecipeAdmin)
admin.site.register(Ingredient)
admin.site.register(Instruction)
admin.site.register(Tool)
如果你遇到这样的错误。
确保在添加到模型后迁移数据库。