Ejemplo n.º 1
0
    def retrieve_objects(self, model_object):
        """Retrieve the objects of the many part.

        A Mayn2One relation uses the repository manager to retrieve
        a list of objects.

        """
        from model.functions import get_pkey_values
        value = get_pkey_values(model_object)
        if len(value) == 1:
            value = value[0]

        field = self.inverse.related_field
        repository = self.inverse.model._repository
        repository_manager = repository.data_connector.repository_manager
        return repository_manager.find_matching_objects(field, value)
Ejemplo n.º 2
0
    def set_related(self, obj, new_obj):
        """Set the related field.

        The value should be a model object.

        """
        from model.functions import get_pkey_values
        attribute_name = self.attribute_name
        model = self.foreign_model
        if new_obj is None:
            new_value = None
        else:
            if not isinstance(new_obj, model):
                raise ValueError("try to affect {} to the {}.{} model " \
                        "field".format(new_obj, self.model, self.field_name))

            values = get_pkey_values(new_obj)
            if len(values) == 1:
                values = values[0]

            new_value = values

        setattr(obj, attribute_name, new_value)