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
예제 #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) 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
예제 #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,
            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
예제 #4
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
예제 #5
0
    def test_get_remote_field_with_new_django_version(self):
        class MockField(object):
            remote_field = 'example_remote_field'

        compat.django.VERSION = (1, 10)
        assert compat.get_remote_field(
            MockField(), default='default_value') == 'example_remote_field'
        assert compat.get_remote_field(
            object(), default='default_value') == 'default_value'
예제 #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 _get_fields(opts):
    fields = OrderedDict()
    for field in [
            field for field in opts.fields
            if field.serialize and not get_remote_field(field)
    ]:
        fields[field.name] = field

    return fields
예제 #9
0
def _get_reverse_relationships(opts):
    """
    Returns an `OrderedDict` of field names to `RelationInfo`.
    """
    # Note that we have a hack here to handle internal API differences for
    # this internal API across Django 1.7 -> Django 1.8.
    # See: https://code.djangoproject.com/ticket/24208

    reverse_relations = OrderedDict()
    all_related_objects = [
        r for r in opts.related_objects if not r.field.many_to_many
    ]
    for relation in all_related_objects:
        accessor_name = relation.get_accessor_name()
        related = getattr(relation, 'related_model', relation.model)
        reverse_relations[accessor_name] = RelationInfo(
            model_field=None,
            related_model=related,
            to_many=get_remote_field(relation.field).multiple,
            to_field=_get_to_field(relation.field),
            has_through_model=False,
            reverse=True)

    # Deal with reverse many-to-many relationships.
    all_related_many_to_many_objects = [
        r for r in opts.related_objects if r.field.many_to_many
    ]
    for relation in all_related_many_to_many_objects:
        accessor_name = relation.get_accessor_name()
        related = getattr(relation, 'related_model', relation.model)
        reverse_relations[accessor_name] = RelationInfo(
            model_field=None,
            related_model=related,
            to_many=True,
            # manytomany do not have to_fields
            to_field=None,
            has_through_model=((getattr(get_remote_field(relation.field),
                                        'through', None) is not None)
                               and not get_remote_field(
                                   relation.field).through._meta.auto_created),
            reverse=True)

    return reverse_relations
예제 #10
0
def _get_reverse_relationships(opts):
    """
    Returns an `OrderedDict` of field names to `RelationInfo`.
    """
    # Note that we have a hack here to handle internal API differences for
    # this internal API across Django 1.7 -> Django 1.8.
    # See: https://code.djangoproject.com/ticket/24208

    reverse_relations = OrderedDict()
    all_related_objects = [r for r in opts.related_objects if not r.field.many_to_many]
    for relation in all_related_objects:
        accessor_name = relation.get_accessor_name()
        related = getattr(relation, 'related_model', relation.model)
        reverse_relations[accessor_name] = RelationInfo(
            model_field=None,
            related_model=related,
            to_many=get_remote_field(relation.field).multiple,
            to_field=_get_to_field(relation.field),
            has_through_model=False,
            reverse=True
        )

    # Deal with reverse many-to-many relationships.
    all_related_many_to_many_objects = [r for r in opts.related_objects if r.field.many_to_many]
    for relation in all_related_many_to_many_objects:
        accessor_name = relation.get_accessor_name()
        related = getattr(relation, 'related_model', relation.model)
        reverse_relations[accessor_name] = RelationInfo(
            model_field=None,
            related_model=related,
            to_many=True,
            # manytomany do not have to_fields
            to_field=None,
            has_through_model=(
                (getattr(get_remote_field(relation.field), 'through', None) is not None) and
                not get_remote_field(relation.field).through._meta.auto_created
            ),
            reverse=True
        )

    return reverse_relations
예제 #11
0
def _get_fields(opts):
    fields = OrderedDict()
    for field in [field for field in opts.fields if field.serialize and not get_remote_field(field)]:
        fields[field.name] = field

    return fields
예제 #12
0
 def test_get_remote_field_with_new_django_version(self):
     class MockField(object):
         remote_field = 'example_remote_field'
     compat.django.VERSION = (1, 10)
     assert compat.get_remote_field(MockField(), default='default_value') == 'example_remote_field'
     assert compat.get_remote_field(object(), default='default_value') == 'default_value'