The following code was working in python 3.10 but not in 3.11 due to a change in the enum
module.
Now the app won’t launch with the following error message :
File "/home/runner/work/e/e/certification/models.py", line 3, in <module>
from .certifications.models import Certification, QuizzCertification # noqa: F401
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/e/e/certification/certifications/models.py", line 17, in <module>
class CertificationType(models.TextChoices):
File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/django/db/models/enums.py", line 49, in __new__
cls = super().__new__(metacls, classname, bases, classdict, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/enum.py", line 560, in __new__
raise exc
File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/enum.py", line 259, in __set_name__
enum_member = enum_class._new_member_(enum_class, *args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/enum.py", line 1278, in __new__
raise TypeError('%r is not a string' % (values[0], ))
TypeError: <class 'certification.certifications.models.QuizzCertification'> is not a string
How would you implement a model field with choices, where the first element is a Model class?
class QuizzCertification(models.Model):
...
class OtherCertification(models.Model):
...
class CertificationType(models.TextChoices):
QUIZZ = QuizzCertification, "Quizz"
OTHER = OtherCertification, "Other"
class Certification(models.Model):
name = models.CharField(max_length=100, unique=True)
description = models.TextField(_("Description"), blank=True, null=True)
type_of = models.CharField(
_("Type of certification"),
max_length=100,
choices=CertificationType,
default=CertificationType.QUIZZ,
)