我有一个班级:
class MyCustomType(object):
a, b = 0, ""
def __init__(self, a, b):
self.a = a
self.b = b
我想为MyCustomType
创建一个自定义字段类。
class MyCustomField(models.Field):
@staticmethod
def parse_value(value):
# magic
return MyCustomType(a, b)
def from_db_value(self, value, expression, connection):
if value is None:
return value
return self.parse_value(value)
def to_python(self, value):
if isinstance(value, MyCustomType) or value is None:
return value
return self.parse_value(value)
如何使用两个数据库字段存储a
和b
数据? a
是一个整数,b
是一个字符串吗?