I used Python version 3.11.3 Django version 4.2.3
And I tested multiselectfield in simple project.
models.py
from django.db import models
from multiselectfield import MultiSelectField
class Book(models.Model):
BOOK_CHOICES = (
('at','at'),
('at2','at2'),
('at3','at3'),
('at4','at4')
)
title=MultiSelectField(choices=BOOK_CHOICES)
admin.py
from django.contrib import admin
from .models import Book
admin.site.register(Book)
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'data',
'multiselectfield',
]
Error
File "C:\Users\Meliksah Can\Desktop\book\manage.py", line 22, in <module>
main()
File "C:\Users\Meliksah Can\Desktop\book\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Meliksah Can\Desktop\book\venv\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
utility.execute()
File "C:\Users\Meliksah Can\Desktop\book\venv\Lib\site-packages\django\core\management\__init__.py", line 416, in execute
django.setup()
File "C:\Users\Meliksah Can\Desktop\book\venv\Lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Meliksah Can\Desktop\book\venv\Lib\site-packages\django\apps\registry.py", line 116, in populate
app_config.import_models()
File "C:\Users\Meliksah Can\Desktop\book\venv\Lib\site-packages\django\apps\config.py", line 269, in import_models
self.models_module = import_module(models_module_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Meliksah Can\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\Meliksah Can\Desktop\book\data\models.py", line 5, in <module>
class Book(models.Model):
File "C:\Users\Meliksah Can\Desktop\book\data\models.py", line 12, in Book
title=MultiSelectField(choices=BOOK_CHOICES)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Meliksah Can\Desktop\book\venv\Lib\site-packages\multiselectfield\db\fields.py", line 72, in __init__
self.validators[0] = MaxValueMultiFieldValidator(self.max_length)
~~~~~~~~~~~~~~~^^^
IndexError: list assignment index out of range
I want to make more than one selection in the Django Admin panel, but I could not skip this part. I will use this feature to add that a book belongs to more than one genre when saving it to the database.
Thank you in advance if anyone has had a similar problem or has any suggestions.
Edit your model like this:
remove
models.
beforeMultiSelectField
because it is not in the models of Django, it is a new package.