Python peewee.Model() Examples
The following are 30
code examples of peewee.Model().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
peewee
, or try the search function
.
Example #1
Source File: fake_item_ids.py From detdup with MIT License | 6 votes |
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(" "))
Example #2
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 6 votes |
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
Example #3
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 6 votes |
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()
Example #4
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 6 votes |
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)
Example #5
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 6 votes |
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)
Example #6
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 6 votes |
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)])
Example #7
Source File: peewee_async.py From peewee-async with MIT License | 6 votes |
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))
Example #8
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 6 votes |
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')
Example #9
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 6 votes |
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')
Example #10
Source File: peewee_validates.py From peewee-validates with MIT License | 6 votes |
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
Example #11
Source File: new_request_model.py From openrasp-iast with Apache License 2.0 | 6 votes |
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
Example #12
Source File: config_model.py From openrasp-iast with Apache License 2.0 | 6 votes |
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
Example #13
Source File: report_model.py From openrasp-iast with Apache License 2.0 | 6 votes |
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
Example #14
Source File: validate.py From slim with zlib License | 6 votes |
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
Example #15
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 6 votes |
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')
Example #16
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_drop_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() self.test_create_table() self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel'))), [(u'somemodel', (u'id',), True),])
Example #17
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_add_blob_column(self): self.test_create_table() peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.CharField(null=True) another_field = pw.BlobField(null=True) class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual(SomeModel.select().first().another_field, None)
Example #18
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_ignore_existing_table_evolve_command(self): class SomeModel(pw.Model): some_field = pw.TimeField(null=True) class Meta: database = self.db self.evolve_and_check_noop() peeweedbevolve.clear() self.assertEqual(peeweedbevolve.calc_changes(self.db, ignore_tables=['somemodel']), [])
Example #19
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_ignore_table_evolve_command(self): class SomeModel(pw.Model): some_field = pw.TimeField(null=True) class Meta: database = self.db self.assertEqual(peeweedbevolve.calc_changes(self.db, ignore_tables=['somemodel']), [])
Example #20
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_rename_column_add_not_null_constraint(self): self.test_create_table() peeweedbevolve.clear() class SomeModel(pw.Model): some_other_field = pw.CharField(null=False, aka='some_field') class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual(SomeModel.select().first().some_other_field, 'woot') with self.db.atomic() as txn: self.assertRaises(Exception, lambda: SomeModel.create(some_other_field=None))
Example #21
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_add_not_null_constraint(self): self.test_create_table() peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.CharField(null=False) class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual(SomeModel.select().first().some_field, 'woot') with self.db.atomic() as txn: self.assertRaises(Exception, lambda: SomeModel.create(some_field=None))
Example #22
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_add_index(self): self.test_create_table() peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.CharField(index=True, null=True) class Meta: database = self.db 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',), False)])
Example #23
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_add_column_default(self): self.test_create_table() peeweedbevolve.clear() 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') self.assertEqual(SomeModel.get(SomeModel.id==model.id).some_field, 'woot2')
Example #24
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_add_multi_index(self): self.test_create_table() peeweedbevolve.clear() 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)])
Example #25
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_drop_unique(self): class SomeModel(pw.Model): some_field = pw.CharField(unique=True, null=True) class Meta: database = self.db 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',), True)]) peeweedbevolve.clear() self.test_create_table() self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel'))), [(u'somemodel', (u'id',), True),])
Example #26
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_add_unique(self): self.test_create_table() peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.CharField(unique=True, null=True) class Meta: database = self.db 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',), True)])
Example #27
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_drop_index(self): class SomeModel(pw.Model): some_field = pw.CharField(index=True, null=True) class Meta: database = self.db 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',), False)]) peeweedbevolve.clear() self.test_create_table() self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel'))), [(u'somemodel', (u'id',), True),])
Example #28
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_add_index_table_and_column_rename(self): self.test_create_table() peeweedbevolve.clear() class SomeModel2(pw.Model): some_field2 = pw.CharField(index=True, null=True, aka='some_field') class Meta: database = self.db aka = 'somemodel' self.evolve_and_check_noop() self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel2'))), [(u'somemodel2', (u'id',), True), (u'somemodel2', (u'some_field2',), False)])
Example #29
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_add_index_column_rename(self): self.test_create_table() peeweedbevolve.clear() class SomeModel(pw.Model): some_field2 = pw.CharField(index=True, null=True, aka='some_field') class Meta: database = self.db 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_field2',), False)])
Example #30
Source File: test.py From peewee-db-evolve with GNU Lesser General Public License v3.0 | 5 votes |
def test_add_index_table_rename(self): self.test_create_table() peeweedbevolve.clear() class SomeModel2(pw.Model): some_field = pw.CharField(index=True, null=True) class Meta: database = self.db aka = 'somemodel' self.evolve_and_check_noop() self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel2'))), [(u'somemodel2', (u'id',), True), (u'somemodel2', (u'some_field',), False)])