本文整理汇总了Python中peewee.Model方法的典型用法代码示例。如果您正苦于以下问题:Python peewee.Model方法的具体用法?Python peewee.Model怎么用?Python peewee.Model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类peewee
的用法示例。
在下文中一共展示了peewee.Model方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _create_model
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def _create_model(self, db, table_prefix):
"""
创建数据model
"""
meta_dict = {
"database": db,
"table_name": table_prefix + "_" + "Report"
}
meta = type("Meta", (object, ), meta_dict)
model_dict = {
"id": peewee.AutoField(),
"plugin_name": peewee.CharField(max_length=63),
"description": peewee.TextField(),
"rasp_result_list": self.LongTextField(),
"payload_seq": peewee.CharField(unique=True, max_length=63),
"message": peewee.TextField(),
"time": peewee.IntegerField(default=common.get_timestamp),
"upload": peewee.IntegerField(default=0),
"Meta": meta
}
self.Report = type("Report", (peewee.Model, ), model_dict)
return self.Report
示例2: _create_model
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def _create_model(self, db, table_prefix):
"""
创建数据model
"""
meta_dict = {
"database": db,
"table_name": "Config"
}
meta = type("Meta", (object, ), meta_dict)
model_dict = {
"host_port_hash": peewee.CharField(primary_key=True, max_length=63),
"host_port": peewee.TextField(),
"config_json": peewee.TextField(),
"Meta": meta
}
self.Config = type("Config", (peewee.Model, ), model_dict)
return self.Config
示例3: _create_model
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def _create_model(self, db, table_prefix):
"""
创建数据model
"""
meta_dict = {
"database": db,
"table_name": table_prefix + "_" + "ResultList"
}
meta = type("Meta", (object, ), meta_dict)
model_dict = {
"id": peewee.AutoField(),
"data": self.LongTextField(),
# utf8mb4 编码下 1 char = 4 bytes,会导致peewee创建过长的列导致MariaDB产生 1071, Specified key was too long; 错误, max_length不使用255
"data_hash": peewee.CharField(unique=True, max_length=63),
# scan_status含义: 未扫描:0, 已扫描:1, 正在扫描:2, 扫描中出现错误: 3
"scan_status": peewee.IntegerField(default=0),
"time": peewee.IntegerField(default=common.get_timestamp),
"Meta": meta
}
self.ResultList = type("ResultList", (peewee.Model, ), model_dict)
return self.ResultList
示例4: get_pv_model_info
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def get_pv_model_info(model: Union[peewee.Model, Type[peewee.Model]]):
new_model_cls: Type[Model] = type(model.__class__.__name__ + 'Validator', (Model,), {})
foreign_keys = {}
peewee_fields = {}
ret = {
'table_name': get_pv_table_name(model),
'primary_key': get_pv_pk_name(model),
'foreign_keys': foreign_keys,
'data_model': new_model_cls,
'_peewee_fields': peewee_fields
}
for name, field in model._meta.fields.items():
if isinstance(field, peewee.ForeignKeyField):
rm = field.rel_model
name = '%s_id' % name
# TODO: 这里可能会出问题
foreign_keys[name] = [SQLForeignKey(get_pv_table_name(rm), get_pv_pk_name(rm))]
peewee_fields[name] = field
new_model_cls._append_field(name, field_class_to_schematics_field(field))
return ret
示例5: coerce_single_instance
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def coerce_single_instance(lookup_field, value):
"""
Convert from whatever value is given to a scalar value for lookup_field.
If value is a dict, then lookup_field.name is used to get the value from the dict. Example:
lookup_field.name = 'id'
value = {'id': 123, 'name': 'tim'}
returns = 123
If value is a model, then lookup_field.name is extracted from the model. Example:
lookup_field.name = 'id'
value = <User id=123 name='tim'>
returns = 123
Otherwise the value is returned as-is.
:param lookup_field: Peewee model field used for getting name from value.
:param value: Some kind of value (usually a dict, Model instance, or scalar).
"""
if isinstance(value, dict):
return value.get(lookup_field.name)
if isinstance(value, peewee.Model):
return getattr(value, lookup_field.name)
return value
示例6: execute
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def execute(query):
"""Execute *SELECT*, *INSERT*, *UPDATE* or *DELETE* query asyncronously.
:param query: peewee query instance created with ``Model.select()``,
``Model.update()`` etc.
:return: result depends on query type, it's the same as for sync
``query.execute()``
"""
if isinstance(query, (peewee.Select, peewee.ModelCompoundSelectQuery)):
coroutine = select
elif isinstance(query, peewee.Update):
coroutine = update
elif isinstance(query, peewee.Insert):
coroutine = insert
elif isinstance(query, peewee.Delete):
coroutine = delete
else:
coroutine = raw_query
return (await coroutine(query))
示例7: test_add_fk_column
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def test_add_fk_column(self):
class Person(pw.Model):
class Meta:
database = self.db
class Car(pw.Model):
class Meta:
database = self.db
self.evolve_and_check_noop()
peeweedbevolve.unregister(Car)
class Car(pw.Model):
owner = foreign_key(Person, null=False)
class Meta:
database = self.db
self.evolve_and_check_noop()
person = Person.create()
car = Car.create(owner=person)
示例8: test_drop_fk_column
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def test_drop_fk_column(self):
class Person(pw.Model):
class Meta:
database = self.db
class Car(pw.Model):
owner = foreign_key(Person, null=False)
class Meta:
database = self.db
self.evolve_and_check_noop()
person = Person.create()
car = Car.create(owner=person)
peeweedbevolve.unregister(Car)
class Car(pw.Model):
class Meta:
database = self.db
self.evolve_and_check_noop()
示例9: test_change_fk_column_to_int
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def test_change_fk_column_to_int(self):
class Person(pw.Model):
class Meta:
database = self.db
class Car(pw.Model):
owner = foreign_key(Person, null=False)
class Meta:
database = self.db
self.evolve_and_check_noop()
person = Person.create()
car = Car.create(owner=person)
peeweedbevolve.unregister(Car)
class Car(pw.Model):
owner_id = pw.IntegerField(null=False)
class Meta:
database = self.db
self.evolve_and_check_noop()
self.assertEqual(Car.select().first().owner_id, person.id)
Car.create(owner_id=-1) # this should not fail
示例10: test_add_not_null_constraint_with_records_and_default_which_is_function
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def test_add_not_null_constraint_with_records_and_default_which_is_function(self):
class SomeModel(pw.Model):
some_field = pw.CharField(null=True)
class Meta:
database = self.db
self.evolve_and_check_noop()
SomeModel.create(some_field=None)
peeweedbevolve.clear()
def woot():
return 'woot'
class SomeModel(pw.Model):
some_field = pw.CharField(null=False, default=woot)
class Meta:
database = self.db
self.evolve_and_check_noop()
self.assertEqual(SomeModel.select().first().some_field, 'woot')
示例11: test_reorder_multi_index
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def test_reorder_multi_index(self):
class SomeModel(pw.Model):
some_field = pw.CharField(null=True)
class Meta:
database = self.db
indexes = (
(('id', 'some_field'), False),
)
self.evolve_and_check_noop()
self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel'))), [(u'somemodel', (u'id',), True), (u'somemodel', (u'id',u'some_field'), False)])
peeweedbevolve.clear()
class SomeModel(pw.Model):
some_field = pw.CharField(null=True)
class Meta:
database = self.db
indexes = (
(('some_field', 'id'), False),
)
self.evolve_and_check_noop()
self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel'))), [(u'somemodel', (u'id',), True), (u'somemodel', (u'some_field',u'id'), False)])
示例12: test_drop_column_default
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def test_drop_column_default(self):
class SomeModel(pw.Model):
some_field = pw.CharField(null=True, default='woot2')
class Meta:
database = self.db
self.evolve_and_check_noop()
model = SomeModel.create()
self.assertEqual(model.some_field, 'woot2')
peeweedbevolve.clear()
class SomeModel(pw.Model):
some_field = pw.CharField(null=True)
class Meta:
database = self.db
self.evolve_and_check_noop()
model = SomeModel.create()
self.assertEqual(model.some_field, None)
self.assertEqual(SomeModel.get(SomeModel.id==model.id).some_field, None)
示例13: test_dont_drop_table
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def test_dont_drop_table(self):
class SomeModel(pw.Model):
some_field = pw.CharField(null=True)
class Meta:
database = self.db
self.evolve_and_check_noop()
SomeModel.create(some_field='woot')
peeweedbevolve.clear()
class SomeModel(pw.Model):
some_field = pw.CharField(null=True)
class Meta:
database = self.db
evolve = False
self.evolve_and_check_noop()
# doesn't fail because table is still there
SomeModel.create(some_field='woot2')
示例14: test_dont_add_column
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def test_dont_add_column(self):
class SomeModel(pw.Model):
some_field = pw.CharField(null=True)
class Meta:
database = self.db
self.evolve_and_check_noop()
peeweedbevolve.clear()
class SomeModel(pw.Model):
some_field = pw.CharField(null=True)
some_other_field = pw.CharField(null=False)
class Meta:
database = self.db
evolve = False
self.evolve_and_check_noop()
# should not fail because the not-null column wasn't added
SomeModel.create(some_field='woot')
示例15: __init__
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import Model [as 别名]
def __init__(self, data_model):
self.data_model = data_model
self.data_model.fake_item_ids_store = self
assert self.data_model.cache_dir, "FakeItemIds need cache_dir from data_model!"
sqlite_path = os.path.join(self.data_model.cache_dir, "fake_item_ids_store.db")
sqlite_database = SqliteDatabase(sqlite_path, check_same_thread=False)
class FakeItemIdsStore(Model):
is_deleted = BooleanField(default=False) # mark processed or duplicated items
item_id = CharField()
item_content_json = TextField()
created_at = TimeField(default=datetime.datetime.now)
class Meta:
database = sqlite_database
self.storage = FakeItemIdsStore
if not self.storage.table_exists():
self.storage.create_table()
sqlite_database.create_index(self.storage, "is_deleted item_id".split(" "))