本文整理汇总了Python中peewee.ForeignKeyField方法的典型用法代码示例。如果您正苦于以下问题:Python peewee.ForeignKeyField方法的具体用法?Python peewee.ForeignKeyField怎么用?Python peewee.ForeignKeyField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类peewee
的用法示例。
在下文中一共展示了peewee.ForeignKeyField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: models
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def models(Base):
class Hobby(Base):
pass
class User(Base):
hobbies = pw.ManyToManyField(Hobby, backref='users')
class Address(Base):
user = pw.ForeignKeyField(User, backref='addresses')
return Bunch(
Hobby=Hobby,
User=User,
Address=Address,
)
示例2: _to_dict
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def _to_dict(self):
data = {}
fields = self.val._meta.fields
for name, v in model_to_dict(self.val, recurse=False).items():
if isinstance(fields[name], peewee.ForeignKeyField):
name = name + '_id'
elif isinstance(fields[name], peewee.BlobField):
v = get_bytes_from_blob(v)
if self.selected != ALL_COLUMNS and (self.selected and (name not in self.selected)):
continue
data[name] = v
if self.available_columns != ALL_COLUMNS:
return dict_filter(data, self.available_columns)
return data
示例3: get_pv_model_info
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [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
示例4: get_through_model
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def get_through_model(self):
if not self._through_model:
lhs, rhs = self.get_models()
tables = [model._meta.db_table for model in (lhs, rhs)]
class Meta:
database = self.model_class._meta.database
db_table = '%s_%s_through' % tuple(tables)
indexes = (
((lhs._meta.name, rhs._meta.name),
True),)
validate_backrefs = False
attrs = {lhs._meta.name: ForeignKeyField(rel_model=lhs),
rhs._meta.name: ForeignKeyField(rel_model=rhs),
'Meta': Meta}
self._through_model = type(
'%s%sThrough' % (lhs.__name__, rhs.__name__),
(AioModel,),
attrs)
return self._through_model
示例5: test_callable_related_name
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def test_callable_related_name():
class Foo(TestModel):
pass
def rel_name(field):
return '{}_{}_ref'.format(field.model_class._meta.name, field.name)
class Bar(TestModel):
fk1 = ForeignKeyField(Foo, related_name=rel_name)
fk2 = ForeignKeyField(Foo, related_name=rel_name)
class Baz(Bar):
pass
assert Foo.bar_fk1_ref.rel_model is Bar
assert Foo.bar_fk2_ref.rel_model is Bar
assert Foo.baz_fk1_ref.rel_model is Baz
assert Foo.baz_fk2_ref.rel_model is Baz
assert not hasattr(Foo, 'bar_set')
assert not hasattr(Foo, 'baz_set')
示例6: test_object_id_descriptor_naming
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def test_object_id_descriptor_naming():
class Person(Model):
pass
class Foo(Model):
me = ForeignKeyField(Person, db_column='me', related_name='foo1')
another = ForeignKeyField(Person, db_column='_whatever_',
related_name='foo2')
another2 = ForeignKeyField(Person, db_column='person_id',
related_name='foo3')
plain = ForeignKeyField(Person, related_name='foo4')
assert Foo.me is Foo.me_id
assert Foo.another is Foo._whatever_
assert Foo.another2 is Foo.person_id
assert Foo.plain is Foo.plain_id
with pytest.raises(AttributeError):
Foo.another_id
with pytest.raises(AttributeError):
Foo.another2_id
示例7: fields
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def fields(self):
if not self._fields:
self._fields = {}
for name, v in self.val._meta.fields.items():
if isinstance(v, peewee.ForeignKeyField):
name = '%s_id' % name # foreign key
self._fields[name] = v
return self._fields
示例8: field_class_to_schematics_field
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def field_class_to_schematics_field(field: peewee.Field) -> BaseType:
if isinstance(field, peewee.ForeignKeyField):
field = field.rel_field
kwargs = {}
# 检查是否 require
if not ((field.default is not None) or field.null or field.sequence or isinstance(field, peewee.AutoField)):
kwargs['required'] = True
if field.help_text:
kwargs['metadata'] = {'description': field.help_text}
if isinstance(field, peewee.IntegerField):
return IntType(**kwargs)
elif isinstance(field, peewee.FloatField):
return FloatType(**kwargs)
elif isinstance(field, (PG_JSONField, PG_BinaryJSONField, SQLITE_JSONField)):
# 注意 SQLITE_JSONField 是一个 _StringField 所以要提前
return JSONType(**kwargs)
# HStore 貌似才应该对应 dict,json可以对应任意类型
# return JSONDictType(StringType, **kwargs)
elif isinstance(field, peewee.DateTimeField):
return DateTimeType(**kwargs)
elif isinstance(field, peewee.DateField):
return DateType(**kwargs)
elif isinstance(field, peewee._StringField):
return StringType(**kwargs)
elif isinstance(field, peewee.BooleanField):
return BooleanType(**kwargs)
elif isinstance(field, peewee.BlobField):
return BlobType(**kwargs)
elif isinstance(field, PG_ArrayField):
field: PG_ArrayField
return JSONListType(field_class_to_schematics_field(field._ArrayField__field), **kwargs)
# noinspection PyProtectedMember
示例9: test_related_name_collision
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def test_related_name_collision(flushdb):
class Foo(TestModel):
f1 = CharField()
with pytest.raises(AttributeError):
class FooRel(TestModel):
foo = ForeignKeyField(Foo, related_name='f1')
示例10: test_meta_rel_for_model
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def test_meta_rel_for_model():
class User(Model):
pass
class Category(Model):
parent = ForeignKeyField('self')
class Tweet(Model):
user = ForeignKeyField(User)
class Relationship(Model):
from_user = ForeignKeyField(User, related_name='r1')
to_user = ForeignKeyField(User, related_name='r2')
UM = User._meta
CM = Category._meta
TM = Tweet._meta
RM = Relationship._meta
# Simple refs work.
assert UM.rel_for_model(Tweet) is None
assert UM.rel_for_model(Tweet, multi=True) == []
assert UM.reverse_rel_for_model(Tweet) == Tweet.user
assert UM.reverse_rel_for_model(Tweet, multi=True) == [Tweet.user]
# Multi fks.
assert RM.rel_for_model(User) == Relationship.from_user
assert RM.rel_for_model(User, multi=True) == [Relationship.from_user,
Relationship.to_user]
assert UM.reverse_rel_for_model(Relationship) == Relationship.from_user
exp = [Relationship.from_user, Relationship.to_user]
assert UM.reverse_rel_for_model(Relationship, multi=True) == exp
# Self-refs work.
assert CM.rel_for_model(Category) == Category.parent
assert CM.reverse_rel_for_model(Category) == Category.parent
# Field aliases work.
UA = User.alias()
assert TM.rel_for_model(UA) == Tweet.user
示例11: _is_foreign_key
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def _is_foreign_key(field):
return isinstance(field, pw.ForeignKeyField)
示例12: _add_fake_fk_field_hook
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def _add_fake_fk_field_hook():
init = pw.ForeignKeyField.__init__
def _init(*args, **kwargs):
self = args[0]
if 'fake' in kwargs:
self.fake = kwargs['fake']
del kwargs['fake']
init(*args, **kwargs)
pw.ForeignKeyField.__init__ = _init
示例13: foreign_key
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def foreign_key(model, **kwargs):
if PW3:
return pw.ForeignKeyField(model=model, **kwargs)
else:
return pw.ForeignKeyField(rel_model=model, **kwargs)
示例14: DeferredForeignKey
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def DeferredForeignKey(*args):
pw.ForeignKeyField(pw.DeferredRelation(*args))
示例15: test_circular_deps
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def test_circular_deps(self):
class SomeModel(pw.Model):
some_model2 = DeferredForeignKey('SomeModel2')
class Meta:
database = self.db
class SomeModel2(pw.Model):
some_model = pw.ForeignKeyField(SomeModel)
class Meta:
database = self.db
self.evolve_and_check_noop()