我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用peewee.Field()。
def test_get_fields(self): handler = PeeweeHandler(User) fields_objs, fields_names = handler.get_fields() self.assertIsInstance(fields_objs, list) self.assertIsInstance(fields_names, list) expected_fields = ["id", "name", "username", "password_hash", "email", "visits", "description"] self.assertCountEqual(fields_names, expected_fields) for field in fields_objs: self.assertIsInstance(field, Field) self.assertIsInstance(fields_objs[fields_names.index("id")], IntegerField) self.assertIsInstance(fields_objs[fields_names.index("name")], CharField) self.assertIsInstance(fields_objs[fields_names.index("username")], CharField) self.assertIsInstance(fields_objs[fields_names.index("password_hash")], BlobField) self.assertIsInstance(fields_objs[fields_names.index("email")], CharField) self.assertIsInstance(fields_objs[fields_names.index("visits")], IntegerField) self.assertIsInstance(fields_objs[fields_names.index("description")], CharField)
def order(self): if self.sort_field and isinstance(self.sort_field, Field): if self.sort_order and self.sort_field: return self.sort_field elif not self.sort_order and self.sort_field: return -self.sort_field elif self.sort_field: return self.sort_field return None
def get_fields(self): fields = [] fields_names = [] for field_name in dir(self.model): field = getattr(self.model, field_name) if isinstance(field, peewee.Field): fields.append(field) fields_names.append(field_name) return fields, fields_names
def _add_field_hook(): init = pw.Field.__init__ def _init(*args, **kwargs): self = args[0] if 'aka' in kwargs: akas = kwargs['aka'] if hasattr(akas, 'lower'): akas = [akas] self.akas = akas del kwargs['aka'] init(*args, **kwargs) pw.Field.__init__ = _init