Two cases:
1.) I'd like to define an attribute (val) that can take the integers 0, 1, or 2 only.
class Trinary(Model):
"""val should accept the values 0, 1 or 2 only"""
val = IntegerField()
2.) I'd like to define an attribute (val) that can take specific strings only, for example ["strawberry", "peach", "apple"]
class Fruit(Model):
"""val should accept the values "strawberry", "peach" or "apple" only """
val = ???
Is is possible to implement such a restriction using peewee?
Thanks for your help!
Muff
The objects IntegerField
etc. are classes, and can be subclassed (documentation):
The classes should define db_value
to convert from python to database,
and python_value
for the other way round
class TrinaryField(IntegerField):
def db_value(self, value):
if value not in [0,1,2]:
raise TypeError("Non-trinary digit")
return super().db_field(value) # call