可以,作为表创建过程的一部分,填充性别表:
class Gender(Model):
label = CharField(primary_key=True)
class Meta:
database = db
def create_schema():
# Use an execution context to ensure the connection is closed when
# we're finished.
with db.execution_context():
db.create_tables([Gender, ...], True)
if not Gender.select().exists():
defaults = ['MALE', 'FEMALE', 'UNSPECIFIED']
Gender.insert_many([{'label': label} for label in defaults]).execute()
的唯一原因,使性别在这个例子中的表格,不过,会如果您打算动态地添加新的是运行时的性别标签。如果设定值是固定的,我的看法是,你最好做这样的事情:
class Person(Model):
GENDER_MALE = 'M'
GENDER_FEMALE = 'F'
GENDER_UNSPECIFIED = 'U'
name = CharField()
gender = CharField()
# Then, simply:
Person.create(name='Huey', gender=Person.GENDER_MALE)
Person.create(name='Zaizee', gender=Person.GENDER_UNSPECIFIED)
与第一个问题是,它是Stringly类型的,我每次都询问性别,由他们的名字。 第二个不会为我工作。我需要将性别列为表格,因为它也用于其他关系。在这个愚蠢的例子中,我想用性别来标记厕所。 –