Using python and Django, querys problem

Asked

Viewed 108 times

0

I’m using Python with the Django framework. This method must return a resulting query, to send e-mail to the corresponding emails from the database. However, the method works for some cases and for others not.

from django.db.models import Q

def membrosBancaAtual(semestre):
    semestre = semestre.filter(atual = True)
    disciplina = Disciplina.objects.filter(semestre = semestre)
    projetos = ProjetoDeGraduacao.objects.filter(disciplina__in = disciplina)
    query = Q(banca2__in = projetos)
    return query

Does anyone have any idea why some cases work and others do not?

  • query = Q(banca2__in=projetos) wasn’t supposed to be inside a Model.objects.filter()?

  • What do you mean? Can you give an example? I did other functions similar to this one, all had this type and worked when I tested it.

  • You want to send emails to the banking members, you should return the membership list, you’re just returned Q(banca2__in=projetos).

  • But why to an e-mail and not to others? And I also did other methods like this, changing only the "query" line and it worked when I tested, sent the emails. What list of members do you refer to? I’ve been filtering the query with the other variables.

1 answer

0


Your query should be used in a query:

Model.objects.filter(banca2__in=projetos)

. This Return query plays the query to where?

  • So, but on this line query = Q(banca2__in = projetos) I thought I should use Q object, because it serves more complex querys, so I read the documentation of Django. For other methods that I’ve done, which are also querys that select something from the database and then email these people, they’re done in the same way.

  • This Return goes to this filter: QUERYLIST = {
 '1':['Enviar e-mail para alunos Matriculados no semestre atual',alunosMatriculadosAtual],
 '2':['Enviar e-mail para alunos inscritos (projeto submetido)',alunosInscritosAtual],
 '3':['Enviar e-mail para docentes Responsaveis do semestre atual',docentesResponsaveisAtual],
 '4':['Enviar e-mail para supervisores do semestre atual',supervisoresResponsaveisAtual],
 '5':['Enviar e-mail para orientadores do semestre atual',orientadoresResponsaveisAtual],
 '6':['Enviar e-mail para membros da banca do semestre atual',membrosBancaAtual]
 }

  • Using his suggestion, Django gave this return as error: global name 'Model' is not defined

  • I also used Banca.objects.filter(banca2__in=projetos), that Banking is the name of the table and gave the memso error.

  • This function uses the chosen filter option: def options():
 list =[]
 for key,option in QUERYLIST.iteritems():
 list.append((key,option[0]))
 OPCOES = tuple(list)
 return OPCOES

  • This other one also uses the filter: def send_emails(options,departamento, assunto = None,corpo = None):
 if options != []:
 options = literal_eval(options)
 query = Q()
 semestre = Semestre.objects.filter(grupo = departamento)
 for item in options:
 f = QUERYLIST[item][1]
 query.add(f(semestre), query.OR)
 usuarios = User().objects.filter(query)
 dispatch(usuarios,assunto, corpo)

  • The Model of my example is for you to replace by the model you are using, in case Banca. This error of global name 'Banca' is not defined, is because you probably didn’t import the model up there. `from sua_app.models import Banca'

  • Thanks man. It was just this line that was missing in the method. Now it’s working!!!

  • Mark the answer as ok :)

  • I can not click the arrow up. I understood lack reputation for me. Where I managed to click, I do not know if it is the Ok

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.