[英]Django: I'm stuck with the admin register when dealing with foreign keys and many to many fields inside that foreign key model
I have a model Workout , WorkoutDay and Exercise .我有一个model锻炼、锻炼日和锻炼。
A workout can have multiple workoutdays.一次锻炼可以有多个锻炼日。 A workoutday consists of multiple exercises.
一个锻炼日包括多项练习。 Each exercise can be added to multiple workoutdays.
每个锻炼可以添加到多个锻炼日。 Between the relation of workoutday and exercise, there needs to be extra fields included (like amount_required).
在锻炼日和锻炼的关系之间,需要包含额外的字段(如 amount_required)。
What I would like to have in the admin view is: An admin user can add a new workout (with some fields), can add multiple workoutdays, adding multiple exercises to 1 workoutday.我想在管理员视图中拥有的是:管理员用户可以添加一个新的锻炼(带有一些字段),可以添加多个锻炼日,将多个锻炼添加到 1 个锻炼日。
I already tried to make Workoutday an inline adminModel.我已经尝试让 Workoutday 成为内联 adminModel。 That works, but I cannot have exercises included in this way.
那行得通,但我不能以这种方式包含练习。
To visualise it more:为了更形象化:
Workout 1:锻炼 1:
Workoutday1:
Exercise1 - amount_required: 10
Exercise2 - amount_required: 50
Workoutday2:
Exercise1 - amount_required: 12
Workoutday3:
Exercise2 - amount_required: 20
Exercise3 - amount_required: 20
Can somebody help me please?有人可以帮我吗?
Django does not support nested inline admin(define inlines on InlineModelAdmin classes), ticket here. Django 不支持嵌套的内联管理(在InlineModelAdmin类上定义内联),请点击此处。 Take a look of this app though: django-nested-admin
不过看看这个应用程序: django-nested-admin
You can try something like this without using 3rd party apps:您可以在不使用 3rd 方应用程序的情况下尝试这样的事情:
@admin.register(WorkoutDay)
class WorkoutDayAdmin(admin):
fieldsets = [
("Exercices", {"fields": ["exercises"], "classes": ["collapse"]}),
]
Normally it should enable you to select and create multiple exercices
(assuming the field is called exercices
.通常它应该使您能够 select 并创建多个
exercices
(假设该字段称为exercices
。
Are you looking for something like this?你在寻找这样的东西吗?
class ExercisesInline(admin.TabularInline):
model = Exercise
class WorkoutAdmin(admin.ModelAdmin):
list_display = ['id',. .... "your fields" ]
list_filter = ["list of filters"]
inlines = [ExercisesInline]
admin.site.register(Workout, WorkoutAdmin)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.