Issue
I tried to use Django Multiselectfield library but i got this error:
I already installed it properly by command: pip install django-multiselectfield
I also added in my settings.py
INSTALLED_APPS = [
.....
'multiselectfield',
]
My models.py:
from multiselectfield import MultiSelectField
from django.db import models
CATEGORY = [
('CP', 'Computer Programming'),
('LG', 'Language'),
('MA', 'Mathematics'),
('BM', 'Business Management'),
('GK', 'General Knowledge')
]
class Quiz(models.Model):
...
title = models.CharField(max_length=256)
category = MultiSelectField(choices=CATEGORY, max_choices=2, max_length=5)
...
def save(self, *args, **kwargs):
if not self.category:
self.category = ['GK']
super(Quiz, self).save(*args, **kwargs)
def __str__(self):
return self.title
The error: 'super' object has no attribute '_get_flatchoices'
However this is located in the Multiselectfield library code. Because of this error, even my data cannot be saved. I tried to find out what am I missing, but I have no idea.
Any idea what's wrong with my code? Appreciate your help ^_^
Solution
The current version of Multiselectfield is incompatible with Django 5.0. There is no _get_flatchoices in django's CharField anymore, instead Django provides flatchoices directly. Therefore, the following code in db/fields.py should be removed.
def _get_flatchoices(self):
flat_choices = super(MultiSelectField, self)._get_flatchoices()
class MSFFlatchoices(list):
# Used to trick django.contrib.admin.utils.display_for_field into
# not treating the list of values as a dictionary key (which errors
# out)
def __bool__(self):
return False
__nonzero__ = __bool__
return MSFFlatchoices(flat_choices)
flatchoices = property(_get_flatchoices)
Thanks for aliceni81
For further information, you can check on https://github.com/goinnn/django-multiselectfield/issues/141
Answered By - Dra-Code
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.