Opened 13 years ago

Closed 13 years ago

#16624 closed Bug (invalid)

select_related does not return the objects related

Reported by: eguevara2012@… Owned by: nobody
Component: Core (Serialization) Version: 1.3
Severity: Normal Keywords: queryset
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello!
I cannot get the related objects of my class Endereco:

class Endereco(models.Model):

# other fields
imovel = models.ForeignKey(Imovel, blank=True, null=True)

Without using the select_related:

def search_imovel(request):

if request.method == 'POST':

form = SearchImovelForm(request.POST)
if form.is_valid():

quartos = form.cleaned_dataquartos

results = Imovel.objects.filter(quartosexact = quartos)


if results:

return render_to_response('imobiliaria/index/results_imovel.html',{'results':results}, context_instance=RequestContext(request))

This is the output:

[<Imovel: Apartamento na Barra da Tijuca, 150000.00, 2>]
SELECT "imovel_imovel"."id", "imovel_imovel"."codigo", "imovel_imovel"."titulo", "imovel_imovel"."area", "imovel_imovel"."medida", "imovel_imovel"."data_inclusao", "imovel_imovel"."valor", "imovel_imovel"."descricao", "imovel_imovel"."quartos", "imovel_imovel"."destaque", "imovel_imovel"."slug", "imovel_imovel"."cliente_id", "imovel_imovel"."modalidade_id", "imovel_imovel"."status_id", "imovel_imovel"."tipo_id" FROM "imovel_imovel" WHERE "imovel_imovel"."quartos" = 2

Now, using select_related():

def search_imovel(request):

if request.method == 'POST':

form = SearchImovelForm(request.POST)
if form.is_valid():

quartos = form.cleaned_dataquartos

results = Endereco.objects.select_related('imovel').filter(imovelquartosexact = quartos)


if results:

return render_to_response('imobiliaria/index/results_imovel.html',{'results':results}, context_instance=RequestContext(request))

This is the output:

[<Endereco: Av. Juliano Peixoto, 455, Centro, São Paulo, SP>]
SELECT "endereco_endereco"."id", "endereco_endereco"."logradouro", "endereco_endereco"."numero", "endereco_endereco"."complemento", "endereco_endereco"."bairro", "endereco_endereco"."cidade", "endereco_endereco"."estado", "endereco_endereco"."cep", "endereco_endereco"."pais", "endereco_endereco"."imovel_id", "endereco_endereco"."cliente_id", "imovel_imovel"."id", "imovel_imovel"."codigo", "imovel_imovel"."titulo", "imovel_imovel"."area", "imovel_imovel"."medida", "imovel_imovel"."data_inclusao", "imovel_imovel"."valor", "imovel_imovel"."descricao", "imovel_imovel"."quartos", "imovel_imovel"."destaque", "imovel_imovel"."slug", "imovel_imovel"."cliente_id", "imovel_imovel"."modalidade_id", "imovel_imovel"."status_id", "imovel_imovel"."tipo_id" FROM "endereco_endereco" LEFT OUTER JOIN "imovel_imovel" ON ("endereco_endereco"."imovel_id" = "imovel_imovel"."id") WHERE "imovel_imovel"."quartos" = 2

It takes only endereco object and ignores the imovel object.
Does anyone know why this behavior?
Regards!

Change History (3)

comment:1 by eguevara2012@…, 13 years ago

comment:2 by anonymous, 13 years ago

Sorry, text format fail:

Hello!
I cannot get the related objects of my class Endereco:

class Endereco(models.Model):
    # other fields
    imovel = models.ForeignKey(Imovel, blank=True, null=True)

Without using the select_related:

def search_imovel(request): 
    if request.method == 'POST':
        form = SearchImovelForm(request.POST)
        if form.is_valid():            
            quartos = form.cleaned_data['quartos']

            results = Imovel.objects.filter(quartos__exact = quartos)
            
            if results:
                return render_to_response('imobiliaria/index/results_imovel.html',{'results':results}, context_instance=RequestContext(request))

This is the output:

[<Imovel: Apartamento na Barra da Tijuca, 150000.00, 2>]
SELECT "imovel_imovel"."id", "imovel_imovel"."codigo", "imovel_imovel"."titulo", "imovel_imovel"."area", "imovel_imovel"."medida", "imovel_imovel"."data_inclusao", "imovel_imovel"."valor", "imovel_imovel"."descricao", "imovel_imovel"."quartos", "imovel_imovel"."destaque", "imovel_imovel"."slug", "imovel_imovel"."cliente_id", "imovel_imovel"."modalidade_id", "imovel_imovel"."status_id", "imovel_imovel"."tipo_id" FROM "imovel_imovel" WHERE "imovel_imovel"."quartos" = 2 

Now, using select_related():

def search_imovel(request): 
    if request.method == 'POST':
        form = SearchImovelForm(request.POST)
        if form.is_valid():            
            quartos = form.cleaned_data['quartos']

            results = Endereco.objects.select_related('imovel').filter(imovel__quartos__exact = quartos)
            
            if results:
                return render_to_response('imobiliaria/index/results_imovel.html',{'results':results}, context_instance=RequestContext(request))

This is the output:

[<Endereco: Av. Juliano Peixoto, 455, Centro, São Paulo, SP>]
SELECT "endereco_endereco"."id", "endereco_endereco"."logradouro", "endereco_endereco"."numero", "endereco_endereco"."complemento", "endereco_endereco"."bairro", "endereco_endereco"."cidade", "endereco_endereco"."estado", "endereco_endereco"."cep", "endereco_endereco"."pais", "endereco_endereco"."imovel_id", "endereco_endereco"."cliente_id", "imovel_imovel"."id", "imovel_imovel"."codigo", "imovel_imovel"."titulo", "imovel_imovel"."area", "imovel_imovel"."medida", "imovel_imovel"."data_inclusao", "imovel_imovel"."valor", "imovel_imovel"."descricao", "imovel_imovel"."quartos", "imovel_imovel"."destaque", "imovel_imovel"."slug", "imovel_imovel"."cliente_id", "imovel_imovel"."modalidade_id", "imovel_imovel"."status_id", "imovel_imovel"."tipo_id" FROM "endereco_endereco" LEFT OUTER JOIN "imovel_imovel" ON ("endereco_endereco"."imovel_id" = "imovel_imovel"."id") WHERE "imovel_imovel"."quartos" = 2 

It takes only endereco object and ignores the imovel object.
Does anyone know why this behavior?
Regards!

comment:3 by Aymeric Augustin, 13 years ago

Resolution: invalid
Status: newclosed

For general help with Django, please post to the django-users mailing list or ask on the #django IRC channel. Trac is a database of known bugs in Django, not a support forum.

Here, you're seeing the expected behavior of select_related(). The second SQL query has an additional JOIN. The imovel object is reachable as endereco.imovel, for any endereco in results, without another SQL query.

Note: See TracTickets for help on using tickets.
Back to Top