首页 > 解决方案 > 'list_display[5]' 的值不能是 ManyToManyField

问题描述

我正在尝试在其中一个类中创建多对多字段,但出现错误“'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')

标签: djangodjango-modelsdjango-forms

解决方案


你可以发布你的 admin.py 吗?特别是andon.admin.WorkStation?

有关管理控制台中的ManytoManyField 用法,请参阅 Django 文档。

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

在此处输入图像描述


推荐阅读