def compute_objs_diff(self, data, objs, instance): """ Find out the right number of related objects to be saved, given the data and the objects that are already present. :param data: list of data for sub forms :param objs: objs that are already there :param instance: a model instance """ objs_to_delete = [] diff = len(data) - len(objs) # manage obj addition if diff > 0: kwargs = {self.origin_model_name: instance} objs = chain( objs, repeat_lambda(lambda: self.foreign_model(**kwargs), diff) ) # manage obj deletion elif diff < 0: objs, objs_to_delete = isplit(objs, len(data)) return objs, objs_to_delete
def test_isplit(self): _in = [1, 2, 3] q, r = python.isplit(_in, 2) q, r = list(q), list(r) self.assertEqual(q, [1, 2]) self.assertEqual(r, [3])