Django - (admin.E015) 'exclude' 的值包含重复字段

Django - (admin.E015) The value of 'exclude' contains duplicate field(s)

我有以下型号:

class Step(models.Model):
    start_time = models.TimeField()
    time = models.IntegerField()
    schedule = models.ForeignKey(Schedule, on_delete=models.CASCADE)

class Schedule(models.Model):
    identifier = models.CharField(max_length=10)
    name = models.CharField(max_length=100)
    steps = models.ManyToManyField('manager.Step', related_name='steps')

这些注册到 django-admin 如下:

@admin.register(Schedule)
class ScheduleAdmin(admin.ModelAdmin):
    list_display = ['identifier', 'name']


@admin.register(Step)
class StepAdmin(admin.ModelAdmin):
    list_display = ['start_time', 'time', 'schedule']
    exclude = list('schedule')

然而,当 运行 服务器出现以下错误时:

ERRORS:
<class 'manager.admin.StepAdmin'>: (admin.E015) The value of 'exclude' contains duplicate field(s).

我该如何解决这个问题?我不熟悉 Django 中的双向绑定应该如何工作?

您应该将列表写成:

@admin.register(Step)
class StepAdmin(admin.ModelAdmin):
    list_display = ['start_time', 'time', 'schedule']
    exclude = <strong>['schedule']</strong>

通过使用 list('schedule'),它会将列表视为可迭代的,并生成一个列表:

>>> list('schedule')
['s', 'c', 'h', 'e', 'd', 'u', 'l', 'e']