当前位置: 首页>>代码示例>>Python>>正文


Python peewee.ForeignKeyField方法代码示例

本文整理汇总了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,
    ) 
开发者ID:jmcarp,项目名称:nplusone,代码行数:18,代码来源:test_peewee.py

示例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 
开发者ID:fy0,项目名称:slim,代码行数:18,代码来源:data_record.py

示例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 
开发者ID:fy0,项目名称:slim,代码行数:26,代码来源:validate.py

示例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 
开发者ID:kszucs,项目名称:aiopeewee,代码行数:25,代码来源:fields.py

示例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') 
开发者ID:kszucs,项目名称:aiopeewee,代码行数:22,代码来源:test_models.py

示例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 
开发者ID:kszucs,项目名称:aiopeewee,代码行数:24,代码来源:test_models.py

示例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 
开发者ID:fy0,项目名称:slim,代码行数:10,代码来源:data_record.py

示例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 
开发者ID:fy0,项目名称:slim,代码行数:40,代码来源:validate.py

示例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') 
开发者ID:kszucs,项目名称:aiopeewee,代码行数:9,代码来源:test_models.py

示例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 
开发者ID:kszucs,项目名称:aiopeewee,代码行数:41,代码来源:test_models.py

示例11: _is_foreign_key

# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def _is_foreign_key(field):
    return isinstance(field, pw.ForeignKeyField) 
开发者ID:keredson,项目名称:peewee-db-evolve,代码行数:4,代码来源:peeweedbevolve.py

示例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 
开发者ID:keredson,项目名称:peewee-db-evolve,代码行数:11,代码来源:peeweedbevolve.py

示例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) 
开发者ID:keredson,项目名称:peewee-db-evolve,代码行数:7,代码来源:test.py

示例14: DeferredForeignKey

# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import ForeignKeyField [as 别名]
def DeferredForeignKey(*args):
    pw.ForeignKeyField(pw.DeferredRelation(*args)) 
开发者ID:keredson,项目名称:peewee-db-evolve,代码行数:4,代码来源:test.py

示例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() 
开发者ID:keredson,项目名称:peewee-db-evolve,代码行数:12,代码来源:test.py


注:本文中的peewee.ForeignKeyField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。