Ensure NULL-able Enums don't raise peewee.IntegrityError

This commit is contained in:
Evgeny Chernyavskiy 2022-07-20 14:42:44 -04:00 committed by Ethan Paul
parent 44ad5da339
commit dac554e8d4
1 changed files with 5 additions and 1 deletions

View File

@ -367,7 +367,11 @@ class EnumField(peewee.CharField): # pylint: disable=abstract-method
def python_value(self, value: str) -> enum.Enum:
try:
return self.enumeration[super().python_value(value)]
return (
None
if value is None and self.null
else self.enumeration[super().python_value(value)]
)
except KeyError:
raise peewee.IntegrityError(
f"Enum {self.enumeration.__name__} has no value with name '{value}'"