【问题标题】:The value of 'list_display[5]' must not be a ManyToManyField'list_display[5]' 的值不能是 ManyToManyField
【发布时间】:2020-07-07 06:11:23
【问题描述】:

我正在尝试在其中一个类中创建多对多字段,我收到错误消息“'list_display[5]' 的值不能是 ManyToManyField”

需要帮助,在此先感谢 :)

class ShiftConfig(models.Model):
    description = models.CharField(max_length=30)
    start_time = models.TimeField()
    end_time = models.TimeField()

    def __str__(self):
        return str(self.id) + ' : ' + str(self.start_time)

class FaultConfig(models.Model):
    description = models.CharField(max_length=30)
    message = models.ForeignKey(Message, null=True, on_delete=models.SET_NULL)
    recipients = models.ForeignKey(UserGroup, null=True, on_delete=models.SET_NULL)
    alert_time = models.DurationField(default=timedelta(0.0001))
    repeat = models.PositiveSmallIntegerField()
    escalated_fault = models.ForeignKey('self', null=True, on_delete=models.SET_NULL, blank=True)

    def __str__(self):
        return str(self.id) + ' : ' + str(self.description)

这是相关的类。

class WorkStation(models.Model):
    name = models.CharField(max_length=30)
    location = models.CharField(max_length=30)
    department= models.ForeignKey(Department, null=True, on_delete=models.SET_NULL)
    current_user=models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
    allowed_fault_configs = models.ManyToManyField(FaultConfig, through='WFMembership', through_fields=('workstation', 'fault_config'))
    allowed_shift_configs = models.ManyToManyField(ShiftConfig, through='WSMembership', through_fields=('workstation', 'shift_config'))

    def __str__(self):
        return str(self.id) + ' : ' + str(self.name)


class WFMembership(models.Model):
    workstation = models.ForeignKey(WorkStation, on_delete=models.CASCADE)
    fault_config = models.ForeignKey(FaultConfig, on_delete=models.CASCADE)


class WSMembership(models.Model):
    workstation = models.ForeignKey(WorkStation, on_delete=models.CASCADE)
    shift_config = models.ForeignKey(ShiftConfig, on_delete=models.CASCADE)

这是提到该字段不能是 ManyToManyField 的错误

    Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Program Files\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Program Files\Python36\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\channels\management\commands\runserver.py", line 69, in inner_run
    self.check(display_num_errors=True)
  File "C:\Program Files\Python36\lib\site-packages\django\core\management\base.py", line 441, in check
    raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
<class 'andon.admin.WorkStation'>: (admin.E109) The value of 'list_display[5]' must not be a ManyToManyField.
<class 'andon.admin.WorkStation'>: (admin.E109) The value of 'list_display[6]' must not be a ManyToManyField.

System check identified 2 issues (0 silenced).

这是工作站的 admin.py

@admin.register(WorkStation)
class WorkStation(admin.ModelAdmin):
    list_display = ('id', 'name','location','department','current_user','allowed_fault_configs', 'allowed_shift_configs')
    list_display_links = ('id', 'name')

【问题讨论】:

  • 请分享完整的错误堆栈跟踪和发生错误的代码
  • 我关注了similar question,它能够解决我的问题

标签: django django-models django-forms


【解决方案1】:

你能发布你的 admin.py 吗?具体是andon.admin.WorkStation?

请参阅管理控制台中ManytoManyField usage 的Django 文档。

您可以编写一个自定义函数来从 ManyToManyField 中检索这些值。

【讨论】:

  • 我已经添加了详细信息
  • 它的 allowed_fault_configs 和 allowed_shift_configs 抱怨。我将使用您可以使用的示例代码更新我的答案。但我建议查看我在答案中发布的链接以获得一些背景和理解。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
  • 2017-07-04
  • 2017-07-18
  • 1970-01-01
  • 2016-01-05
  • 2013-03-07
相关资源
最近更新 更多