def adjust_participacao(new_participacao, old): composicao = Composicao() composicao.comissao, composicao.periodo = [ get_fk_related(Composicao._meta.get_field(name), value) for name, value in (('comissao', old.cod_comissao), ('periodo', old.cod_periodo_comp))] # check if there is already an "equal" one in the db already_created = Composicao.objects.filter( comissao=composicao.comissao, periodo=composicao.periodo) if already_created: assert len(already_created) == 1 # we must never have made 2 copies [composicao] = already_created else: composicao.save() new_participacao.composicao = composicao
def get_participacao_composicao(obj, value): # value parameter is ignored new = Composicao() for new_field, value in [('comissao', obj.cod_comissao), ('periodo', obj.cod_periodo_comp)]: model_field = Composicao._meta.get_field(new_field) value = get_related_if_foreignkey(model_field, '???', value) setattr(new, new_field, value) previous = Composicao.objects.filter(comissao=new.comissao, periodo=new.periodo) if previous: assert len(previous) == 1 return previous[0] else: new.save() return new
def adjust_participacao(new_participacao, old): composicao = Composicao() composicao.comissao, composicao.periodo = [ get_fk_related(Composicao._meta.get_field(name), value) for name, value in (('comissao', old.cod_comissao), ('periodo', old.cod_periodo_comp)) ] # check if there is already an "equal" one in the db already_created = Composicao.objects.filter(comissao=composicao.comissao, periodo=composicao.periodo) if already_created: assert len(already_created) == 1 # we must never have made 2 copies [composicao] = already_created else: composicao.save() new_participacao.composicao = composicao