I have Python Enum
class like this:
from enum import Enum
class Seniority(Enum):
Intern = "Intern"
Junior_Engineer = "Junior Engineer"
Medior_Engineer = "Medior Engineer"
Senior_Engineer = "Senior Engineer"
In MYSQL database, seniority ENUM column has values "Intern", "Junior Engineer", "Medior Engineer", "Senior Engineer".
The problem is that I get an error:
LookupError: "Junior Engineer" is not among the defined enum values
This error has occurred when I call query like:
UserProperty.query.filter_by(full_name='John Doe').first()
seniority
is enum property in the UserProperty
model.
class UserProperty(db.Model):
...
seniority = db.Column(db.Enum(Seniority), nullable=True)
...
For this class I've defined Schema class using marshmallow
Schema
and EnumField
from marshmallow_enum
package:
class UserPropertySchema(Schema):
...
seniority = EnumField(Seniority, by_value=True)
...
What to do in this situation, because I can't define python class property name with space. How to force python to use values of defined properties instead of property names?
As Shenanigator stated in the comment of my question, we can use aliases to solve this problem.
Seniority = Enum(
value='Seniority',
names=[
('Intern', 'Intern'),
('Junior Engineer', 'Junior Engineer'),
('Junior_Engineer', 'Junior_Engineer'),
('Medior Engineer', 'Medior Engineer'),
('Medior_Engineer', 'Medior_Engineer'),
('Senior Engineer', 'Senior Engineer'),
('Senior_Engineer', 'Senior_Engineer')
]
)
A list is a lot of emblematic names (individuals) bound to one of a kind, steady qualities. Inside a specification, the individuals can be looked at by character, and the count itself can be iterated over.
Module Contents
This module characterizes four specification classes that can be utilized to characterize remarkable arrangements of names and qualities: Enum, IntEnum, Flag, and IntFlag. It likewise characterizes one decorator, one of a kind(), and one aide, auto.
class enum.Enum
Base class for making listed constants. See segment Functional API for an other development grammar.
class enum.IntEnum
Base class for making listed constants that are additionally subclasses of int.
class enum.IntFlag
Base class for making listed constants that can be consolidated utilizing the bitwise administrators without losing their IntFlag participation. IntFlag individuals are additionally subclasses of int.
class enum.Flag
Base class for making listed constants that can be consolidated utilizing the bitwise tasks without losing their Flag enrollment.
enum.unique()
Enum class decorator that guarantees just one name is bound to any one worth.
class enum.auto
Examples are supplanted with a suitable incentive for Enum individuals. Beginning worth beginnings at 1.
New in variant 3.6: Flag, IntFlag, a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With