django - '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')
解决方案
你可以发布你的 admin.py 吗?特别是andon.admin.WorkStation?
有关管理控制台中的ManytoManyField 用法,请参阅 Django 文档。
您可以编写一个自定义函数来从 ManyToManyField 中检索这些值。
推荐阅读
- python - Sphinx 乳胶方程式警告:内联乳胶退出并出现错误 [stdout] 没有文件 math.aux
- javascript - 在前端 javascript 函数中使用 Scriptlet 代码
- discord - 如何在 discord.js 中停止循环
- javascript - TypeError: jquery__WEBPACK_IMPORTED_MODULE_7___default(...)(...).dropdown 不是函数 - reactjs 下拉菜单
- html - 我是 CSS 新手,如何改进此代码
- rust - 当 PartialOrd 更细微并返回 Option 时,为什么 PartialEq 返回一个布尔值
? - nosql - 用于数值字段的 DynamoDB GSI 与 LSI
- binance - 从我通过 Brownie 部署到 BSC 的测试智能合约中移除资金
- vue.js - 如何渲染自定义组件
引导 Vue?(Vue.js 2) - flutter - 使用 GoogleFonts textTheme 时出现 Flutter !_needsLayout 错误