google-code-export / wadofstuff

Automatically exported from code.google.com/p/wadofstuff
1 stars 0 forks source link

Serialization of related objets using filter() in queryset #20

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hello!
The serialization of related objects not work using filter() in queryset.

## models

class Pessoa(models.Model):    
    email = models.CharField(max_length=255)
    #others fields

class Cliente(Pessoa):  
    nome = models.CharField(max_length=255) 
    #others fields 

## views

def search_cliente(request, nome): 
    if request.is_ajax():
        clientes = Cliente.objects.prefetch_related('pessoa').filter(nome__icontains=nome)
        data = serializers.serialize("json", clientes, relations={'pessoa':{'fields':('email',)}}, indent=4, ensure_ascii=False)
        return HttpResponse(data, mimetype="application/json")

## query output in console:

SELECT `pessoa_pessoa`.`id`, `pessoa_pessoa`.`email`, 
`pessoa_pessoa`.`telefone`, `pessoa_pessoa`.`celular`, 
`pessoa_pessoa`.`data_inclusao`, `pessoa_cliente`.`nome`, 
`pessoa_cliente`.`cpf`, `pessoa_cliente`.`nome_fantasia`, 
`pessoa_cliente`.`razao_social`, `pessoa_cliente`.`cnpj`, 
`pessoa_cliente`.`pessoa_id` FROM `pessoa_cliente` INNER JOIN `pessoa_pessoa` 
ON (`pessoa_cliente`.`pessoa_id` = `pessoa_pessoa`.`id`) WHERE 
`pessoa_cliente`.`nome` LIKE %silvio% 

## output json in Firebug:

[
    {
        "pk": 2, 
        "model": "pessoa.cliente", 
        "fields": {
            "nome_fantasia": "Empresa Fantasma", 
            "cnpj": "10.781.916/0001-80", 
            "razao_social": "Fantasma LTDA", 
            "cpf": "297.276.931-72", 
            "nome": "Silvio"
        }
    }, 
]

The email field in "Pessoa" class not appear in serialization, even using the 
parameter select_related() or new parameter prefetch_related() in Django 1.4.
I use the version 1.1.0 of this serializer.
Thanks!

Original issue reported on code.google.com by eguevara...@gmail.com on 3 Apr 2012 at 12:14