Exemplo n.º 1
0
def _get_forward_relationships(opts):
    """
    Returns an `OrderedDict` of field names to `RelationInfo`.
    """
    forward_relations = OrderedDict()
    forwards_fields = [
        field for field in opts.fields
        if field.serialize and get_remote_field(field)
    ]
    for field in forwards_fields:
        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.
    many_to_many_fields = [
        field for field in opts.many_to_many if field.serialize
    ]
    for field in many_to_many_fields:
        forward_relations[field.name] = RelationInfo(
            model_field=field,
            related_model=get_related_model(field),
            to_many=True,
            # many-to-many do not have to_fields
            to_field=None,
            has_through_model=not get_remote_field(
                field).through._meta.auto_created  # NOQA
        )

    return forward_relations
Exemplo n.º 2
0
def _get_reverse_relationships(mapper):
    """
    Returns an `OrderedDict` of field names to `RelationInfo`.

    """
    reverse_relations = OrderedDict()

    # Foreign keys (many to one)
    reverse_fk_relations = _get_relations(mapper, MANYTOONE)
    for field_name, relationship_property in reverse_fk_relations:
        reverse_relations[field_name] = RelationInfo(
            model_field=relationship_property,
            related_model=mapper.class_,
            to_many=False,
            to_field=_get_to_field(relationship_property),
            has_through_model=False)

    # Many to many
    m2m_relations = _get_relations(mapper, MANYTOMANY)
    for field_name, relationship_property in m2m_relations:
        reverse_relations[field_name] = RelationInfo(
            model_field=relationship_property,
            related_model=mapper.class_,
            to_many=True,
            # manytomany do not have to_fields
            to_field=None,
            has_through_model=True)

    return reverse_relations
Exemplo n.º 3
0
 def test_get_relation_kwargs_for_unique_fk_field_(self):
     relation_info = RelationInfo(
         model_field=self.Book._meta.get_field('publisher'),
         related_model=self.Book,
         to_many=True,
         to_field=self.Publisher._meta.get_field('id'),
         has_through_model=False
     )
     kwargs = get_relation_kwargs('publisher', relation_info)
     validators = kwargs.pop('validators')
     self.assertEqual(
         kwargs,
         {
             'allow_empty': False,
             'label': 'Publisher',
             'many': True,
             'queryset': self.Book._default_manager,
             'to_field': self.Publisher._meta.get_field('id'),
             'view_name': 'book-detail'
         }
     )
     self.assertEqual(len(validators), 1)
     self.assertIsInstance(validators[0], UniqueValidator)
     self.assertEqual(
         validators[0].queryset,
         self.Book._default_manager
     )
Exemplo n.º 4
0
 def test_get_nested_relation_kwargs_default(self):
     relation_info = RelationInfo(model_field=Column(
         'birthday_place', Integer),
                                  related_model=self.FakeUserModel,
                                  to_many=False,
                                  to_field=Column('address', Integer),
                                  has_through_model=False)
     self.assertEqual(get_nested_relation_kwargs(relation_info),
                      {'read_only': True})
Exemplo n.º 5
0
 def test_get_nested_relation_kwargs_for_list_field(self):
     relation_info = RelationInfo(
         model_field=Column('addresses', Integer),
         related_model=self.FakeUserModel,
         to_many=True,
         to_field=Column('id', ForeignKey('FakeAddressModel.id')),
         has_through_model=True
     )
     self.assertEqual(
         get_nested_relation_kwargs(relation_info),
         {'read_only': True, 'many': True}
     )
Exemplo n.º 6
0
 def test_get_nested_relation_kwargs_for_one_to_one_field(self):
     relation_info = RelationInfo(
         model_field=self.PublisherAddress._meta.get_field('publisher'),
         related_model=self.PublisherAddress,
         to_many=False,
         to_field=self.Publisher._meta.get_field('id'),
         has_through_model=False
     )
     self.assertEqual(
         get_nested_relation_kwargs(relation_info),
         {'read_only': True}
     )
Exemplo n.º 7
0
def _get_reverse_relationships(opts):
    """
    Returns an `OrderedDict` of field names to `RelationInfo`.
    """
    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)

    # 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:
        has_through_model = False
        through = getattr(get_remote_field(relation.field), 'through', None)
        if through is not None:
            remote_field = get_remote_field(relation.field)
            has_through_model = not remote_field.through._meta.auto_created

        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,
            # many-to-many do not have to_fields
            to_field=None,
            has_through_model=has_through_model)

    return reverse_relations
Exemplo n.º 8
0
 def test_get_relation_kwargs_for_m2m_field(self):
     relation_info = RelationInfo(
         model_field=self.FakeProductModel.orders,
         related_model=self.FakeProductModel,
         to_many=True,
         to_field=self.FakeM2MProductOrderModel.order_id.field,
         has_through_model=True)
     self.assertEqual(
         get_relation_kwargs('orders', relation_info), {
             'many': True,
             'read_only': True,
             'to_field': self.FakeM2MProductOrderModel.order_id.field,
             'view_name': 'product-detail'
         })
Exemplo n.º 9
0
 def test_get_relation_kwargs_for_foreign_key(self):
     relation_info = RelationInfo(
         model_field=self.FakeM2MProductOrderModel.product_id,
         related_model=self.FakeProductModel,
         to_many=False,
         to_field=self.FakeProductModel.id.field,
         has_through_model=False)
     self.assertEqual(
         get_relation_kwargs('product_id', relation_info), {
             'label': 'Product_id',
             'queryset': self.FakeProductModel,
             'to_field': self.FakeProductModel.id.field,
             'view_name': 'product-detail'
         })
Exemplo n.º 10
0
 def test_get_nested_relation_kwargs_for_m2m_field(self):
     relation_info = RelationInfo(
         model_field=self.Author._meta.get_field('id'),
         related_model=self.Author,
         to_many=True,
         to_field=self.Book._meta.get_field('id'),
         has_through_model=True
     )
     self.assertEqual(
         get_nested_relation_kwargs(relation_info),
         {
             'read_only': True,
             'many': True
         }
     )
Exemplo n.º 11
0
def _get_forward_relationships(mapper):
    """
    Returns an `OrderedDict` of field names to `RelationInfo`.
    """
    forward_relations = OrderedDict()

    # Foreign keys (one to many)
    fk_relations = _get_relations(mapper, ONETOMANY)
    for field_name, relationship_property in fk_relations:
        forward_relations[field_name] = RelationInfo(
            model_field=relationship_property,
            related_model=mapper.class_,
            to_many=False,
            to_field=_get_to_field(relationship_property),
            has_through_model=False)

    return forward_relations
Exemplo n.º 12
0
 def test_get_relation_kwargs_for_reverse_m2m_field(self):
     relation_info = RelationInfo(
         model_field=self.Author._meta.get_field('id'),
         related_model=self.Author,
         to_many=True,
         to_field=self.Book._meta.get_field('id'),
         has_through_model=True
     )
     self.assertEqual(
         get_relation_kwargs('id', relation_info),
         {
             'label': 'Id',
             'many': True,
             'read_only': True,
             'to_field': self.Book._meta.get_field('id'),
             'view_name': 'author-detail'
         }
     )
Exemplo n.º 13
0
 def test_get_relation_kwargs_for_nullable_fk_field_with_validators(self):
     model_field = self.PublisherAddress._meta.get_field('publisher')
     relation_info = RelationInfo(
         model_field=model_field,
         related_model=self.PublisherAddress,
         to_many=True,
         to_field=self.Publisher._meta.get_field('id'),
         has_through_model=False
     )
     self.assertEqual(
         get_relation_kwargs('publisher', relation_info),
         {
             'allow_null': True,
             'label': 'Publisher',
             'many': True,
             'queryset': self.PublisherAddress._default_manager,
             'required': False,
             'to_field': self.Publisher._meta.get_field('id'),
             'validators': model_field.validators,
             'view_name': 'publisheraddress-detail'
         }
     )