コード例 #1
0
def _get_forward_relationships(opts):
    """
    Returns an `OrderedDict` of field names to `RelationInfo`.
    """
    forward_relations = OrderedDict()
    for field in [
        field for field in opts.fields
        if field.serialize and get_remote_field(field) and not (field.primary_key and field.one_to_one)
        # If the field is a OneToOneField and it's been marked as PK, then this
        # is a multi-table inheritance auto created PK ('%_ptr').
    ]:
        forward_relations[field.name] = RelationInfo(
            model_field=field,
            related_model=get_related_model(field),
            to_many=False,
            to_field=_get_to_field(field),
            has_through_model=False,
            reverse=False
        )

    # Deal with forward many-to-many relationships.
    for field in [field for field in opts.many_to_many if field.serialize]:
        forward_relations[field.name] = RelationInfo(
            model_field=field,
            related_model=get_related_model(field),
            to_many=True,
            # manytomany do not have to_fields
            to_field=None,
            has_through_model=(
                not get_remote_field(field).through._meta.auto_created
            ),
            reverse=False
        )

    return forward_relations
コード例 #2
0
def _get_forward_relationships(opts):
    """
    Returns an `OrderedDict` of field names to `RelationInfo`.
    """
    forward_relations = OrderedDict()
    for field in [
            field for field in opts.fields
            if field.serialize and get_remote_field(field)
    ]:
        forward_relations[field.name] = RelationInfo(
            model_field=field,
            related_model=get_related_model(field),
            to_many=False,
            to_field=_get_to_field(field),
            has_through_model=False,
            reverse=False)

    # Deal with forward many-to-many relationships.
    for field in [field for field in opts.many_to_many if field.serialize]:
        forward_relations[field.name] = RelationInfo(
            model_field=field,
            related_model=get_related_model(field),
            to_many=True,
            # manytomany do not have to_fields
            to_field=None,
            has_through_model=(
                not get_remote_field(field).through._meta.auto_created),
            reverse=False)

    return forward_relations
コード例 #3
0
def _get_forward_relationships(opts):
    """
    Returns an `OrderedDict` of field names to `RelationInfo`.
    """
    forward_relations = OrderedDict()
    for field in [field for field in opts.fields if field.serialize and get_remote_field(field)]:
        forward_relations[field.name] = RelationInfo(
            model_field=field,
            related_model=get_related_model(field),
            to_many=False,
            to_field=_get_to_field(field),
            has_through_model=False,
        )

    # Deal with forward many-to-many relationships.
    for field in [field for field in opts.many_to_many if field.serialize]:
        forward_relations[field.name] = RelationInfo(
            model_field=field,
            related_model=get_related_model(field),
            to_many=True,
            # manytomany do not have to_fields
            to_field=None,
            has_through_model=(not get_remote_field(field).through._meta.auto_created),
        )

    return forward_relations
コード例 #4
0
ファイル: model_meta.py プロジェクト: kafura0/OranKids
def _get_forward_relationships(opts):
    """
    Returns an `OrderedDict` of field names to `RelationInfo`.
    """
    forward_relations = OrderedDict()
    for field in [
            field for field in opts.fields
            if field.serialize and get_remote_field(field)
            and not (field.primary_key and field.one_to_one)
            # If the field is a OneToOneField and it's been marked as PK, then this
            # is a multi-table inheritance auto created PK ('%_ptr').
    ]:
        forward_relations[field.name] = RelationInfo(
            model_field=field,
            related_model=get_related_model(field),
            to_many=False,
            to_field=_get_to_field(field),
            has_through_model=False,
            reverse=False)

    # Deal with forward many-to-many relationships.
    for field in [field for field in opts.many_to_many if field.serialize]:
        forward_relations[field.name] = RelationInfo(
            model_field=field,
            related_model=get_related_model(field),
            to_many=True,
            # manytomany do not have to_fields
            to_field=None,
            has_through_model=(
                not get_remote_field(field).through._meta.auto_created),
            reverse=False)

    return forward_relations
コード例 #5
0
def convert_field_to_list_or_connection(field,
                                        registry=None,
                                        input_flag=None,
                                        nested_fields=False):
    model = get_related_model(field)

    def dynamic_type():
        if input_flag and not nested_fields:
            return DjangoListField(ID,
                                   required=is_required(field)
                                   and input_flag == 'create')

        _type = registry.get_type_for_model(model, for_input=input_flag)
        if not _type:
            return

        if _type._meta.filter_fields and input_flag:
            return DjangoFilterListField(_type,
                                         required=is_required(field)
                                         and input_flag == 'create')
        return DjangoListField(_type,
                               required=is_required(field)
                               and input_flag == 'create')

    return Dynamic(dynamic_type)
コード例 #6
0
def _get_pk(opts):
    pk = opts.pk
    rel = get_remote_field(pk)

    while rel and rel.parent_link:
        # If model is a child via multi-table inheritance, use parent's pk.
        pk = get_related_model(pk)._meta.pk
        rel = get_remote_field(pk)

    return pk
コード例 #7
0
def _get_pk(opts):
    pk = opts.pk
    rel = get_remote_field(pk)

    while rel and rel.parent_link:
        # If model is a child via multi-table inheritance, use parent's pk.
        pk = get_related_model(pk)._meta.pk
        rel = get_remote_field(pk)

    return pk
コード例 #8
0
def convert_field_to_djangomodel(field,
                                 registry=None,
                                 input_flag=None,
                                 nested_fields=False):
    model = get_related_model(field)

    def dynamic_type():
        # Avoid create field for auto generate OneToOneField product of an inheritance
        if isinstance(field, models.OneToOneField) and issubclass(
                field.model, field.related_model):
            return
        if input_flag and not nested_fields:
            return ID(description=field.help_text or field.verbose_name,
                      required=is_required(field) and input_flag == 'create')

        _type = registry.get_type_for_model(model, for_input=input_flag)
        if not _type:
            return

        return Field(_type,
                     description=field.help_text or field.verbose_name,
                     required=is_required(field) and input_flag == 'create')

    return Dynamic(dynamic_type)