From dac554e8d48c22a01b401a8a9a4caa019a2e7935 Mon Sep 17 00:00:00 2001 From: Evgeny Chernyavskiy Date: Wed, 20 Jul 2022 14:42:44 -0400 Subject: [PATCH] Ensure NULL-able Enums don't raise peewee.IntegrityError --- peewee_plus.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/peewee_plus.py b/peewee_plus.py index 70758a9..a2ec021 100644 --- a/peewee_plus.py +++ b/peewee_plus.py @@ -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}'"