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

Time:03-16

I have the following Models:

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')

These are registered to django-admin as follows:

@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')

However when running the server I get the following error:

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

How can I resolve this issue? I am unfamiliar with how Two way binding in Django is supposed to work?

CodePudding user response:

You should write the list as:

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

By using list('schedule'), it will see the list as an iterable, and make a list:

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