Python django.db.models.fields.related 模块,ForeignObjectRel() 实例源码

我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用django.db.models.fields.related.ForeignObjectRel()

项目:State-TalentMAP-API    作者:18F    | 项目源码 | 文件源码
def is_valid_field(self, model, field):
        # Split with maximum splits of 1, so if passed xx__yy__zz, we get [xx, yy__zz]
        components = field.split(LOOKUP_SEP, 1)
        try:
            field = model._meta.get_field(components[0])

            # Reverse lookup
            if isinstance(field, ForeignObjectRel):
                return self.is_valid_field(field.model, components[1])

            if field.get_internal_type() in self.related_field_types and len(components) > 1:
                return self.is_valid_field(field.related_model, components[1])

            return True
        except FieldDoesNotExist:
            return False
项目:LIMS-Backend    作者:LeafLIMS    | 项目源码 | 文件源码
def is_valid_field(self, model, field):
        """
        Return true if the field exists within the model (or in the related
        model specified using the Django ORM __ notation)
        """
        components = field.split('__', 1)
        try:
            field, parent_model, direct, m2m = \
                model._meta.get_field_by_name(components[0])

            # reverse relation
            if isinstance(field, ForeignObjectRel):
                return self.is_valid_field(field.model, components[1])

            # foreign key
            if field.rel and len(components) == 2:
                return self.is_valid_field(field.rel.to, components[1])
            return True
        except FieldDoesNotExist:
            return False
项目:Tinychat-Bot--Discontinued    作者:Tinychat    | 项目源码 | 文件源码
def getCustomProperties(self):
        self.fields = {}
        self.relations = {}
        self.columns = []

        self.meta = self.klass._meta

        for name in self.meta.get_all_field_names():
            x = self.meta.get_field_by_name(name)[0]

            if isinstance(x, files.FileField):
                self.readonly_attrs.update([name])

            if isinstance(x, ForeignObjectRel):
                continue

            if isinstance(x, models.ManyToManyField):
                self.relations[name] = x
            elif not isinstance(x, models.ForeignKey):
                self.fields[name] = x
            else:
                self.relations[name] = x

        parent_fields = []

        for field in self.meta.parents.values():
            parent_fields.append(field.attname)
            del self.relations[field.name]

        self.exclude_attrs.update(parent_fields)

        props = self.fields.keys()

        self.encodable_properties.update(props)
        self.decodable_properties.update(props)

        self.exclude_attrs.update(['_state'])
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def select_on_queryset(self, queryset):
        """
        This method runs either prefetch_related or select_related on the queryset
        to improve indexing speed of the relation.

        It decides which method to call based on the number of related objects:
         - single (eg ForeignKey, OneToOne), it runs select_related
         - multiple (eg ManyToMany, reverse ForeignKey) it runs prefetch_related
        """
        try:
            field = self.get_field(queryset.model)
        except FieldDoesNotExist:
            return queryset

        if isinstance(field, RelatedField):
            if field.many_to_one or field.one_to_one:
                queryset = queryset.select_related(self.field_name)
            elif field.one_to_many or field.many_to_many:
                queryset = queryset.prefetch_related(self.field_name)

        elif isinstance(field, ForeignObjectRel):
            # Reverse relation
            if isinstance(field, OneToOneRel):
                # select_related for reverse OneToOneField
                queryset = queryset.select_related(self.field_name)
            else:
                # prefetch_related for anything else (reverse ForeignKey/ManyToManyField)
                queryset = queryset.prefetch_related(self.field_name)

        return queryset
项目:blog_django    作者:chnpmy    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:dream_blog    作者:fanlion    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:MxOnline    作者:myTeemo    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:djangoblog    作者:liuhuipy    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:django-composite-foreignkey    作者:onysos    | 项目源码 | 文件源码
def test_model_fields(self):
        f = Contact._meta.get_field("customer")
        self.assertIsInstance(f, CompositeForeignKey)
        l = Contact._meta.get_fields()
        self.assertIn("customer", [field.name for field in l])

        f2 = Customer._meta.get_field("contacts")
        self.assertIsInstance(f2, ForeignObjectRel)
        l2 = Customer._meta.get_fields()
        self.assertIn("contacts", [field.name for field in l2])

        self.assertIsNone(f.db_type(None))
项目:sdining    作者:Lurance    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:DjangoBlog    作者:0daybug    | 项目源码 | 文件源码
def has_output(self):
        if (isinstance(self.field, ForeignObjectRel) and
                self.field.field.null or hasattr(self.field, 'rel') and
                self.field.null):
            extra = 1
        else:
            extra = 0
        return len(self.lookup_choices) + extra > 1
项目:DjangoBlog    作者:0daybug    | 项目源码 | 文件源码
def choices(self, cl):
        from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
        yield {
            'selected': self.lookup_val is None and not self.lookup_val_isnull,
            'query_string': cl.get_query_string({},
                [self.lookup_kwarg, self.lookup_kwarg_isnull]),
            'display': _('All'),
        }
        for pk_val, val in self.lookup_choices:
            yield {
                'selected': self.lookup_val == smart_text(pk_val),
                'query_string': cl.get_query_string({
                    self.lookup_kwarg: pk_val,
                }, [self.lookup_kwarg_isnull]),
                'display': val,
            }
        if (isinstance(self.field, ForeignObjectRel) and
                (self.field.field.null or isinstance(self.field.field, ManyToManyField)) or
                hasattr(self.field, 'rel') and (self.field.null or isinstance(self.field, ManyToManyField))):
            yield {
                'selected': bool(self.lookup_val_isnull),
                'query_string': cl.get_query_string({
                    self.lookup_kwarg_isnull: 'True',
                }, [self.lookup_kwarg]),
                'display': EMPTY_CHANGELIST_VALUE,
            }
项目:xadmin-markdown-editor    作者:bluenknight    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:trydjango18    作者:lucifer-yqh    | 项目源码 | 文件源码
def has_output(self):
        if (isinstance(self.field, ForeignObjectRel) and
                self.field.field.null or hasattr(self.field, 'rel') and
                self.field.null):
            extra = 1
        else:
            extra = 0
        return len(self.lookup_choices) + extra > 1
项目:trydjango18    作者:lucifer-yqh    | 项目源码 | 文件源码
def choices(self, cl):
        from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
        yield {
            'selected': self.lookup_val is None and not self.lookup_val_isnull,
            'query_string': cl.get_query_string({},
                [self.lookup_kwarg, self.lookup_kwarg_isnull]),
            'display': _('All'),
        }
        for pk_val, val in self.lookup_choices:
            yield {
                'selected': self.lookup_val == smart_text(pk_val),
                'query_string': cl.get_query_string({
                    self.lookup_kwarg: pk_val,
                }, [self.lookup_kwarg_isnull]),
                'display': val,
            }
        if (isinstance(self.field, ForeignObjectRel) and
                (self.field.field.null or isinstance(self.field.field, ManyToManyField)) or
                hasattr(self.field, 'rel') and (self.field.null or isinstance(self.field, ManyToManyField))):
            yield {
                'selected': bool(self.lookup_val_isnull),
                'query_string': cl.get_query_string({
                    self.lookup_kwarg_isnull: 'True',
                }, [self.lookup_kwarg]),
                'display': EMPTY_CHANGELIST_VALUE,
            }
项目:trydjango18    作者:wei0104    | 项目源码 | 文件源码
def has_output(self):
        if (isinstance(self.field, ForeignObjectRel) and
                self.field.field.null or hasattr(self.field, 'rel') and
                self.field.null):
            extra = 1
        else:
            extra = 0
        return len(self.lookup_choices) + extra > 1
项目:trydjango18    作者:wei0104    | 项目源码 | 文件源码
def choices(self, cl):
        from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
        yield {
            'selected': self.lookup_val is None and not self.lookup_val_isnull,
            'query_string': cl.get_query_string({},
                [self.lookup_kwarg, self.lookup_kwarg_isnull]),
            'display': _('All'),
        }
        for pk_val, val in self.lookup_choices:
            yield {
                'selected': self.lookup_val == smart_text(pk_val),
                'query_string': cl.get_query_string({
                    self.lookup_kwarg: pk_val,
                }, [self.lookup_kwarg_isnull]),
                'display': val,
            }
        if (isinstance(self.field, ForeignObjectRel) and
                (self.field.field.null or isinstance(self.field.field, ManyToManyField)) or
                hasattr(self.field, 'rel') and (self.field.null or isinstance(self.field, ManyToManyField))):
            yield {
                'selected': bool(self.lookup_val_isnull),
                'query_string': cl.get_query_string({
                    self.lookup_kwarg_isnull: 'True',
                }, [self.lookup_kwarg]),
                'display': EMPTY_CHANGELIST_VALUE,
            }
项目:eduDjango    作者:yuzhou6    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:Django-IMOOC-Shop    作者:LBruse    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:emitjson    作者:atsuoishimoto    | 项目源码 | 文件源码
def _django_get_all_field_names(model):
    from itertools import chain
    from django.db.models import Manager
    from django.db.models.fields.related import ForeignObjectRel

    return list(set(chain.from_iterable(
        (field.name, field.attname) if hasattr(field, 'attname') else (field.name,)
        for field in model._meta.get_fields()
            if not isinstance(field, ForeignObjectRel)
    )))
项目:StudyOnline    作者:yipwinghong    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:gougo    作者:amaozhao    | 项目源码 | 文件源码
def choices(self, cl):
        # #### MPTT ADDITION START
        try:
            # EMPTY_CHANGELIST_VALUE has been removed in django 1.9
            from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
        except:
            EMPTY_CHANGELIST_VALUE = self.empty_value_display
        # #### MPTT ADDITION END
        yield {
            'selected': self.lookup_val is None and not self.lookup_val_isnull,
            'query_string': cl.get_query_string({}, [self.lookup_kwarg, self.lookup_kwarg_isnull]),
            'display': _('All'),
        }
        for pk_val, val, padding_style in self.lookup_choices:
            yield {
                'selected': self.lookup_val == smart_text(pk_val),
                'query_string': cl.get_query_string({
                    self.lookup_kwarg: pk_val,
                }, [self.lookup_kwarg_isnull]),
                'display': val,
                # #### MPTT ADDITION START
                'padding_style': padding_style,
                # #### MPTT ADDITION END
            }
        if (isinstance(self.field, ForeignObjectRel) and
                (self.field.field.null or isinstance(self.field.field, ManyToManyField)) or
                hasattr(self.field, 'rel') and
                (self.field.null or isinstance(self.field, ManyToManyField))):
            yield {
                'selected': bool(self.lookup_val_isnull),
                'query_string': cl.get_query_string({
                    self.lookup_kwarg_isnull: 'True',
                }, [self.lookup_kwarg]),
                'display': EMPTY_CHANGELIST_VALUE,
            }
项目:xadmin_python3    作者:mahongquan    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field,ForeignObjectRel)
项目:Django-shop    作者:poetries    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:omni-forms    作者:omni-digital    | 项目源码 | 文件源码
def get_model_fields(self):
        """
        Method to get all model fields for the content type
        associated with the forms specified content type

        :return: List of model field instances
        """

        def is_valid_field(field):
            if isinstance(field, (models.AutoField, ForeignObjectRel, GenericRelation, GenericForeignKey)):
                return False
            else:
                return True
        return list(filter(is_valid_field, self.content_type.model_class()._meta.get_fields()))
项目:MoocOnline    作者:My-captain    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:followme    作者:wzqnls    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:mxonline    作者:huwei86    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:geekpoint    作者:Lujinghu    | 项目源码 | 文件源码
def has_output(self):
        if (isinstance(self.field, ForeignObjectRel) and
                self.field.field.null or hasattr(self.field, 'rel') and
                self.field.null):
            extra = 1
        else:
            extra = 0
        return len(self.lookup_choices) + extra > 1
项目:geekpoint    作者:Lujinghu    | 项目源码 | 文件源码
def choices(self, cl):
        from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
        yield {
            'selected': self.lookup_val is None and not self.lookup_val_isnull,
            'query_string': cl.get_query_string({},
                [self.lookup_kwarg, self.lookup_kwarg_isnull]),
            'display': _('All'),
        }
        for pk_val, val in self.lookup_choices:
            yield {
                'selected': self.lookup_val == smart_text(pk_val),
                'query_string': cl.get_query_string({
                    self.lookup_kwarg: pk_val,
                }, [self.lookup_kwarg_isnull]),
                'display': val,
            }
        if (isinstance(self.field, ForeignObjectRel) and
                (self.field.field.null or isinstance(self.field.field, ManyToManyField)) or
                hasattr(self.field, 'rel') and (self.field.null or isinstance(self.field, ManyToManyField))):
            yield {
                'selected': bool(self.lookup_val_isnull),
                'query_string': cl.get_query_string({
                    self.lookup_kwarg_isnull: 'True',
                }, [self.lookup_kwarg]),
                'display': EMPTY_CHANGELIST_VALUE,
            }
项目:Charlotte    作者:LiZoRN    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:imooc-django    作者:zaxlct    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:muxueonline    作者:124608760    | 项目源码 | 文件源码
def is_related_field(field):
    return isinstance(field, ForeignObjectRel)
项目:django-wechat-api    作者:crazy-canux    | 项目源码 | 文件源码
def has_output(self):
        if (isinstance(self.field, ForeignObjectRel) and
                self.field.field.null or hasattr(self.field, 'rel') and
                self.field.null):
            extra = 1
        else:
            extra = 0
        return len(self.lookup_choices) + extra > 1
项目:django-wechat-api    作者:crazy-canux    | 项目源码 | 文件源码
def choices(self, cl):
        from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
        yield {
            'selected': self.lookup_val is None and not self.lookup_val_isnull,
            'query_string': cl.get_query_string({},
                [self.lookup_kwarg, self.lookup_kwarg_isnull]),
            'display': _('All'),
        }
        for pk_val, val in self.lookup_choices:
            yield {
                'selected': self.lookup_val == smart_text(pk_val),
                'query_string': cl.get_query_string({
                    self.lookup_kwarg: pk_val,
                }, [self.lookup_kwarg_isnull]),
                'display': val,
            }
        if (isinstance(self.field, ForeignObjectRel) and
                (self.field.field.null or isinstance(self.field.field, ManyToManyField)) or
                hasattr(self.field, 'rel') and (self.field.null or isinstance(self.field, ManyToManyField))):
            yield {
                'selected': bool(self.lookup_val_isnull),
                'query_string': cl.get_query_string({
                    self.lookup_kwarg_isnull: 'True',
                }, [self.lookup_kwarg]),
                'display': EMPTY_CHANGELIST_VALUE,
            }