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


Python fields.GenericForeignKey方法代码示例

本文整理汇总了Python中django.contrib.contenttypes.fields.GenericForeignKey方法的典型用法代码示例。如果您正苦于以下问题:Python fields.GenericForeignKey方法的具体用法?Python fields.GenericForeignKey怎么用?Python fields.GenericForeignKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.contrib.contenttypes.fields的用法示例。


在下文中一共展示了fields.GenericForeignKey方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _details

# 需要导入模块: from django.contrib.contenttypes import fields [as 别名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 别名]
def _details(self, current_model, relation):
        direct = isinstance(relation, Field) or isinstance(relation, GenericForeignKey)
        model = relation.model._meta.concrete_model
        if model == current_model:
            model = None

        field = relation if direct else relation.field
        return relation, model, direct, bool(field.many_to_many)  # many_to_many can be None 
开发者ID:r4fek,项目名称:django-cassandra-engine,代码行数:10,代码来源:tests.py

示例2: get_related_fields

# 需要导入模块: from django.contrib.contenttypes import fields [as 别名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 别名]
def get_related_fields(model):
    return {
        field.name: field
        for field in model._meta.get_fields()
        if field.is_relation and not isinstance(field, (GenericForeignKey, GenericRel))
    } 
开发者ID:eamigo86,项目名称:graphene-django-extras,代码行数:8,代码来源:utils.py

示例3: test_order_with_respect_to_private_field

# 需要导入模块: from django.contrib.contenttypes import fields [as 别名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 别名]
def test_order_with_respect_to_private_field(self):
        class PrivateFieldModel(models.Model):
            content_type = models.ForeignKey('contenttypes.ContentType', models.CASCADE)
            object_id = models.PositiveIntegerField()
            private = GenericForeignKey()

            class Meta:
                order_with_respect_to = 'private'

        state = ModelState.from_model(PrivateFieldModel)
        self.assertNotIn('order_with_respect_to', state.options) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:13,代码来源:test_state.py

示例4: test_generic_fk

# 需要导入模块: from django.contrib.contenttypes import fields [as 别名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 别名]
def test_generic_fk(self):
        A = self.create_model("A", foreign_keys=[
            models.ForeignKey('B', models.CASCADE),
            GenericForeignKey(),
        ])
        B = self.create_model("B", foreign_keys=[
            models.ForeignKey('C', models.CASCADE),
        ])
        self.assertRelated(A, [B])
        self.assertRelated(B, [A]) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:12,代码来源:test_state.py

示例5: _getattr_for_field_tracking

# 需要导入模块: from django.contrib.contenttypes import fields [as 别名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 别名]
def _getattr_for_field_tracking(self, attr):
        """Avoid N+1 for foreignkeys, store there id instead of value"""
        if isinstance(getattr(self.__class__, attr), GenericForeignKey):  # return the instance for ContentType attributes. Caution, this does N+1
            return getattr(self, attr, None)

        # Try to return ID instead of instance
        return getattr(self, f'{attr}_id', None) or getattr(self, attr, None) 
开发者ID:f213,项目名称:education-backend,代码行数:9,代码来源:models.py

示例6: test_str

# 需要导入模块: from django.contrib.contenttypes import fields [as 别名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 别名]
def test_str(self):
        class Model(models.Model):
            field = GenericForeignKey()
        self.assertEqual(str(Model.field), 'contenttypes_tests.Model.field') 
开发者ID:nesdis,项目名称:djongo,代码行数:6,代码来源:test_fields.py

示例7: _get_generic_fields

# 需要导入模块: from django.contrib.contenttypes import fields [as 别名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 别名]
def _get_generic_fields():
    """
    Return a list of all GenericForeignKeys in all models.
    """
    generic_fields = []
    for model in apps.get_models():
        for field_name, field in model.__dict__.items():
            if isinstance(field, GenericForeignKey):
                generic_fields.append(field)
    return generic_fields 
开发者ID:lgoodridge,项目名称:django-uniauth,代码行数:12,代码来源:merge.py

示例8: restore_object

# 需要导入模块: from django.contrib.contenttypes import fields [as 别名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 别名]
def restore_object(self, attrs, instance=None):
        """
        Restore the model instance.
        """
        m2m_data = {}
        related_data = {}
        nested_forward_relations = {}
        # noinspection PyProtectedMember
        meta = self.opts.model._meta

        # Reverse fk or one-to-one relations
        for (obj, model) in meta.get_all_related_objects_with_model():
            field_name = obj.get_accessor_name()
            if field_name in attrs:
                related_data[field_name] = attrs.pop(field_name)

        # Reverse m2m relations
        for (obj, model) in meta.get_all_related_m2m_objects_with_model():
            field_name = obj.get_accessor_name()
            if field_name in attrs:
                m2m_data[field_name] = attrs.pop(field_name)

        # Forward m2m relations
        if issubclass(meta.many_to_many.__class__, tuple):
            temp_m2m = list(meta.many_to_many)
        else:
            temp_m2m = meta.many_to_many
        for field in temp_m2m + meta.virtual_fields:
            if isinstance(field, GenericForeignKey):
                continue
            if field.name in attrs:
                m2m_data[field.name] = attrs.pop(field.name)

        # Nested forward relations - These need to be marked so we can save
        # them before saving the parent model instance.
        for field_name in attrs.keys():
            if isinstance(self.fields.get(field_name, None), Serializer):
                nested_forward_relations[field_name] = attrs[field_name]

        # Create an empty instance of the model
        if instance is None:
            instance = self.opts.model()

        for key, val in attrs.items():
            try:
                setattr(instance, key, val)
            except ValueError:
                self._errors[key] = [self.error_messages['required']]

        # Any relations that cannot be set until we've
        # saved the model get hidden away on these
        # private attributes, so we can deal with them
        # at the point of save.
        instance._related_data = related_data
        instance._m2m_data = m2m_data
        instance._nested_forward_relations = nested_forward_relations

        return instance 
开发者ID:erigones,项目名称:esdc-ce,代码行数:60,代码来源:serializers.py

示例9: get_expected_types

# 需要导入模块: from django.contrib.contenttypes import fields [as 别名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 别名]
def get_expected_types(self, api: TypeChecker, model_cls: Type[Model], *, method: str) -> Dict[str, MypyType]:
        from django.contrib.contenttypes.fields import GenericForeignKey

        expected_types = {}
        # add pk if not abstract=True
        if not model_cls._meta.abstract:
            primary_key_field = self.get_primary_key_field(model_cls)
            field_set_type = self.get_field_set_type(api, primary_key_field, method=method)
            expected_types['pk'] = field_set_type

        for field in model_cls._meta.get_fields():
            if isinstance(field, Field):
                field_name = field.attname
                field_set_type = self.get_field_set_type(api, field, method=method)
                expected_types[field_name] = field_set_type

                if isinstance(field, ForeignKey):
                    field_name = field.name
                    foreign_key_info = helpers.lookup_class_typeinfo(api, field.__class__)
                    if foreign_key_info is None:
                        # maybe there's no type annotation for the field
                        expected_types[field_name] = AnyType(TypeOfAny.unannotated)
                        continue

                    related_model = self.get_field_related_model_cls(field)
                    if related_model is None:
                        expected_types[field_name] = AnyType(TypeOfAny.from_error)
                        continue

                    if related_model._meta.proxy_for_model is not None:
                        related_model = related_model._meta.proxy_for_model

                    related_model_info = helpers.lookup_class_typeinfo(api, related_model)
                    if related_model_info is None:
                        expected_types[field_name] = AnyType(TypeOfAny.unannotated)
                        continue

                    is_nullable = self.get_field_nullability(field, method)
                    foreign_key_set_type = helpers.get_private_descriptor_type(foreign_key_info,
                                                                               '_pyi_private_set_type',
                                                                               is_nullable=is_nullable)
                    model_set_type = helpers.convert_any_to_type(foreign_key_set_type,
                                                                 Instance(related_model_info, []))

                    expected_types[field_name] = model_set_type

            elif isinstance(field, GenericForeignKey):
                # it's generic, so cannot set specific model
                field_name = field.name
                gfk_info = helpers.lookup_class_typeinfo(api, field.__class__)
                gfk_set_type = helpers.get_private_descriptor_type(gfk_info, '_pyi_private_set_type',
                                                                   is_nullable=True)
                expected_types[field_name] = gfk_set_type

        return expected_types 
开发者ID:typeddjango,项目名称:django-stubs,代码行数:57,代码来源:context.py


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