Python peewee.CharField() Examples

The following are 30 code examples of peewee.CharField(). 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: config_model.py    From openrasp-iast with Apache License 2.0 6 votes vote down vote up
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 #2
Source File: fake_item_ids.py    From detdup with MIT License 6 votes vote down vote up
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 #3
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #4
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #5
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 vote down vote up
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: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #8
Source File: test_models.py    From aiopeewee with MIT License 6 votes vote down vote up
def test_order_by_inheritance():
    class Base(TestModel):
        created = DateTimeField()

        class Meta:
            order_by = ('-created',)

    class Foo(Base):
        data = CharField()

    class Bar(Base):
        val = IntegerField()
        class Meta:
            order_by = ('-val',)

    foo_order_by = Foo._meta.order_by[0]
    assert isinstance(foo_order_by, Field)
    assert foo_order_by.model_class is Foo
    assert foo_order_by.name == 'created'

    bar_order_by = Bar._meta.order_by[0]
    assert isinstance(bar_order_by, Field)
    assert bar_order_by.model_class is Bar
    assert bar_order_by.name == 'val' 
Example #9
Source File: report_model.py    From openrasp-iast with Apache License 2.0 6 votes vote down vote up
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 #10
Source File: test_taxadb.py    From taxadb with MIT License 6 votes vote down vote up
def test_has_index(self):
        """Check method returns False and True when either table or index
        does not exist"""
        from taxadb.schema import BaseModel
        import peewee as pw

        class FooBar(BaseModel):
            id = pw.IntegerField(null=False)
            name = pw.CharField()

        idx = FooBar.index(FooBar.name, name='name')
        FooBar.add_index(idx)
        obj = self._buildTaxaDBObject(TaxaDB)
        # Test returns False
        self.assertFalse(FooBar.has_index(name='foo'))
        FooBar.create_table(fail_silently=True)
        self.assertFalse(FooBar.has_index(name='foo'))
        self.assertFalse(FooBar.has_index())
        self.assertFalse(FooBar.has_index(columns=10))
        # Test returns True
        self.assertTrue(FooBar.has_index(name='name'))
        FooBar.drop_table() 
Example #11
Source File: new_request_model.py    From openrasp-iast with Apache License 2.0 6 votes vote down vote up
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: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #13
Source File: conftest.py    From sanic_crud with MIT License 5 votes vote down vote up
def app(request):
    from peewee import SqliteDatabase, Model, CharField, IntegerField, ForeignKeyField
    from sanic import Sanic
    from sanic.log import log

    from sanic_crud import generate_crud

    db = SqliteDatabase('tests/test.db')

    class BaseModel(Model):
        class Meta:
            database = db

    class Job(BaseModel):
        name = CharField()
        description = CharField()
        base_pay = IntegerField()

    class Person(BaseModel):
        name = CharField()
        job = ForeignKeyField(Job, related_name='person_job', null=True)
        email = CharField()

    db.create_tables([Person, Job])
    job = Job(name='Space garbage man', description='Collects garbage in space', base_pay=15)
    person = Person(name='Sanic the Hedgehog', email='gottagofeast@fast.com', job=1)
    job.save()
    person.save()

    test_app = Sanic(__name__)

    test_app.log = log
    generate_crud(test_app, [Person, Job])

    def final():
        db.drop_tables([Person, Job])

    request.addfinalizer(final)
    return test_app 
Example #14
Source File: test_taxadb.py    From taxadb with MIT License 5 votes vote down vote up
def test_table_exists_failed(self):
        """Check the method throws SystemExit if a table does not exist"""
        from taxadb.schema import BaseModel
        import peewee as pw

        class NotFound(BaseModel):
            id = pw.IntegerField(null=False)
            name = pw.CharField()
        obj = self._buildTaxaDBObject(TaxaDB)
        with self.assertRaises(SystemExit):
            obj.check_table_exists(NotFound) 
Example #15
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_ignore_new_model(self):
    class SomeModel(pw.Model):
      some_field = pw.CharField(null=True)
      class Meta:
        database = self.db
        evolve = False
    self.evolve_and_check_noop()
    with self.assertRaises(pw.ProgrammingError):
      # should fail because table does not exist
      SomeModel.create(some_field='woot') 
Example #16
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_change_column_max_length(self):
    class SomeModel(pw.Model):
      some_field = pw.CharField(default='w', max_length=1)
      class Meta:
        database = self.db
    self.evolve_and_check_noop()
    peeweedbevolve.clear()
    class SomeModel(pw.Model):
      some_field = pw.CharField(default='woot', max_length=4)
      class Meta:
        database = self.db
    self.evolve_and_check_noop()
    model = SomeModel.create()
    self.assertEqual(model.some_field, 'woot')
    self.assertEqual(SomeModel.select().first().some_field, 'woot') 
Example #17
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #18
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #19
Source File: migrations.py    From tildemush with GNU General Public License v3.0 5 votes vote down vote up
def logging_env_column(db, migrator):
    m.migrate(
        migrator.add_column('log', 'env', pw.CharField(null=True))
    ) 
Example #20
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #21
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #22
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #23
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_drop_index_table_rename(self):
    class SomeModel2(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,'somemodel2'))), [(u'somemodel2', (u'id',), True), (u'somemodel2', (u'some_field',), False)])
    peeweedbevolve.clear()
    class SomeModel(pw.Model):
      some_field = pw.CharField(null=True)
      class Meta:
        database = self.db
        aka = 'somemodel2'
    self.evolve_and_check_noop()
    self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel'))), [(u'somemodel', (u'id',), True),]) 
Example #24
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #25
Source File: test_models.py    From aiopeewee with MIT License 5 votes vote down vote up
def test_meta_remove_field():

    class _Model(Model):
        title = CharField(max_length=25)
        content = TextField(default='')

    _Model._meta.remove_field('content')
    assert 'content' not in _Model._meta.fields
    assert 'content' not in _Model._meta.sorted_field_names
    assert [f.name for f in _Model._meta.sorted_fields] == ['id', 'title'] 
Example #26
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_rename_column_aka_list(self):
    self.test_create_table()
    peeweedbevolve.clear()
    class SomeModel(pw.Model):
      some_other_field = pw.CharField(null=True, aka=['some_field'])
      class Meta:
        database = self.db
    self.evolve_and_check_noop()
    self.assertEqual(SomeModel.select().first().some_other_field, 'woot') 
Example #27
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_create_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')
    self.assertEqual(SomeModel.select().first().some_field, 'woot') 
Example #28
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_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()
    self.evolve_and_check_noop()
    with self.assertRaises(pw.ProgrammingError):
      SomeModel.create(some_field='woot2') # fails because table isn't there 
Example #29
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_create_table_with_fk(self):
    class SomeModel(pw.Model):
      some_field = pw.CharField(null=True)
      class Meta:
        database = self.db
    class SomeModel2(pw.Model):
      some_field2 = pw.CharField(null=True)
      some_model = foreign_key(SomeModel)
      class Meta:
        database = self.db
    self.evolve_and_check_noop()
    sm = SomeModel.create(some_field='woot')
    sm2 = SomeModel2.create(some_field2='woot2', some_model=sm) 
Example #30
Source File: test.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_rename_table_aka_string(self):
    self.test_create_table()
    peeweedbevolve.clear()
    class SomeOtherModel(pw.Model):
      some_field = pw.CharField(null=True)
      class Meta:
        database = self.db
        aka = 'somemodel'
    self.evolve_and_check_noop()
    self.assertEqual(SomeOtherModel.select().first().some_field, 'woot')