def create_relationship(self, json_data, relationship_field, related_id_field, view_kwargs):
        """Create a relationship

        :param dict json_data: the request params
        :param str relationship_field: the model attribute used for relationship
        :param str related_id_field: the identifier field of the related model
        :param dict view_kwargs: kwargs from the resource view
        :return boolean: True if relationship have changed else False
        """
        self.before_create_relationship(json_data, relationship_field, related_id_field, view_kwargs)

        obj = self.get_object(view_kwargs)

        if obj is None:
            url_field = getattr(self, 'url_field', 'id')
            filter_value = view_kwargs[url_field]
            raise ObjectNotFound('{}: {} not found'.format(self.model.__name__, filter_value),
                                 source={'parameter': url_field})

        if not hasattr(obj, relationship_field):
            raise RelationNotFound("{} has no attribute {}".format(obj.__class__.__name__, relationship_field))

        related_model = getattr(obj.__class__, relationship_field).property.mapper.class_

        updated = False

        if isinstance(json_data['data'], list):
            obj_ids = {str(getattr(obj__, related_id_field)) for obj__ in getattr(obj, relationship_field)}

            for obj_ in json_data['data']:
                if obj_['id'] not in obj_ids:
                    getattr(obj,
                            relationship_field).append(self.get_related_object(related_model, related_id_field, obj_))
                    updated = True
        else:
            related_object = None

            if json_data['data'] is not None:
                related_object = self.get_related_object(related_model, related_id_field, json_data['data'])

            obj_id = getattr(getattr(obj, relationship_field), related_id_field, None)
            new_obj_id = getattr(related_object, related_id_field, None)
            if obj_id != new_obj_id:
                setattr(obj, relationship_field, related_object)
                updated = True

        try:
            self.session.commit()
        except JsonApiException as e:
            self.session.rollback()
            raise e
        except Exception as e:
            self.session.rollback()
            raise JsonApiException("Create relationship error: " + str(e))

        self.after_create_relationship(obj, updated, json_data, relationship_field, related_id_field, view_kwargs)

        return obj, updated
def create_relationship ( self , json_data , relationship_field , related_id_field , view_kwargs ) : self . before_create_relationship ( json_data , relationship_field , related_id_field , view_kwargs ) obj = self . get_object ( view_kwargs ) if obj is None : url_field = getattr ( self , 'url_field' , 'id' ) filter_value = view_kwargs [ url_field ] raise ObjectNotFound ( '{}: {} not found' . format ( self . model . __name__ , filter_value ) , source = { 'parameter' : url_field } ) if not hasattr ( obj , relationship_field ) : raise RelationNotFound ( "{} has no attribute {}" . format ( obj . __class__ . __name__ , relationship_field ) ) related_model = getattr ( obj . __class__ , relationship_field ) . property . mapper . class_ updated = False if isinstance ( json_data [ 'data' ] , list ) : obj_ids = { str ( getattr ( obj__ , related_id_field ) ) for obj__ in getattr ( obj , relationship_field ) } for obj_ in json_data [ 'data' ] : if obj_ [ 'id' ] not in obj_ids : getattr ( obj , relationship_field ) . append ( self . get_related_object ( related_model , related_id_field , obj_ ) ) updated = True else : related_object = None if json_data [ 'data' ] is not None : related_object = self . get_related_object ( related_model , related_id_field , json_data [ 'data' ] ) obj_id = getattr ( getattr ( obj , relationship_field ) , related_id_field , None ) new_obj_id = getattr ( related_object , related_id_field , None ) if obj_id != new_obj_id : setattr ( obj , relationship_field , related_object ) updated = True try : self . session . commit ( ) except JsonApiException as e : self . session . rollback ( ) raise e except Exception as e : self . session . rollback ( ) raise JsonApiException ( "Create relationship error: " + str ( e ) ) self . after_create_relationship ( obj , updated , json_data , relationship_field , related_id_field , view_kwargs ) return obj , updated
Create a relationship

        :param dict json_data: the request params
        :param str relationship_field: the model attribute used for relationship
        :param str related_id_field: the identifier field of the related model
        :param dict view_kwargs: kwargs from the resource view
        :return boolean: True if relationship have changed else False