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'
         })
 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'
         })
Esempio n. 3
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'
         }
     )
Esempio n. 4
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'
         }
     )
Esempio n. 5
0
    def build_relational_field(self, *args, **kwargs):
        """
        Create fields for forward and reverse relationships.
        """
        field_name, relation_info = args

        field_class = self.serializer_related_field
        field_kwargs = get_relation_kwargs(field_name, relation_info)

        relation_type = relation_info.model_field.direction
        if relation_type in (MANYTOMANY, MANYTOONE, ONETOMANY):
            field_kwargs['many'] = True

        # `to_field` is only valid for slug fields, which aren't
        # supported by SQLAlchemy ORM by default
        field_kwargs.pop('to_field', None)

        # `view_name` is only valid for hyperlinked relationships
        if not issubclass(field_class, HyperlinkedRelatedField):
            field_kwargs.pop('view_name', None)

        return field_class, field_kwargs