1

I am trying to create manytomany fields in one of the class, I am getting an error "The value of 'list_display[5]' must not be a ManyToManyField"

Need Help, Thanks in advance :)

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)

Here is the concerned class.

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)

Here is the error which mentions that the field must not be 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).

Here is the admin.py for Workstation

@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')
Rishi
  • 11
  • 1
  • 3
  • 1
    Please share the full error stacktrace and the code where the error is occuring – ruddra Jul 07 '20 at 06:24
  • I followed [similar question](https://stackoverflow.com/questions/31475104/list-display-for-manytomany-fields-in-admin-panel) and it was able to solve my problem – Rishi Jul 10 '20 at 22:50

1 Answers1

0

Can you post your admin.py? Specifically andon.admin.WorkStation?

Please refer to Django documentation for ManytoManyField usage in admin console.

You can write a custom function to retrieve those values from the ManyToManyField.

enter image description here

Mustafa
  • 48
  • 5
  • I have added the details – Rishi Jul 08 '20 at 05:58
  • Its allowed_fault_configs and allowed_shift_configs that are complaining. I will update my answer with sample code that you could use. But I would recommend going over the link I posted in the answer to gain some background and understanding. – Mustafa Jul 08 '20 at 18:42