Opened 5 years ago

Closed 16 months ago

#16127 closed Bug (worksforme)

Queryset which uses defer() method not serialize

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

Description (last modified by Ramiro Morales)

Hello!
I have this function using json and a queryset with defer() method, but the fields not target in queryset, i need only "id" and "nome":

models.py

class Pessoa(models.Model):
    nome = models.CharField(max_length=50)
    data_nascimento = models.DateField()
    data_inclusao = models.DateField()
    cpf = models.CharField(max_length=14)
    telefone = models.CharField(max_length=13)
    celular = models.CharField(max_length=13)
    
    class Meta:
        abstract = True

class Proprietario(Pessoa):  
    endereco = models.ForeignKey(Endereco)

    def __unicode__(self):
            return "%s, %s" % (self.nome, self.cpf)

views.py:

def load_proprietarios(request):
    data = serializers.serialize('json',Proprietario.objects.defer('cpf','telefone','celular','data_nascimento','data_inclusao',),ensure_ascii=False))
    return HttpResponse(data, mimetype="application/javascript")

This is the output:

[{"pk": 1, "model": "proprietario.proprietario_deferred_celular_cpf_telefone_data_inclusao_data_nascimento", "fields": {}}]

Note that the "id" and the "nome" fields (class Proprietario / class abstract Pessoa) does not appear after defer() method.

Regards.

Change History (6)

comment:1 Changed 5 years ago by Ramiro Morales

Component: Database layer (models, ORM)Core (Serialization)
Description: modified (diff)
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 5 years ago by knutz3n

UI/UX: unset

This bug report should be simplified with a simpler model and view as given below.

models.py:

class Person(models.Model):
    name = models.CharField(max_length=50)
    phone = models.CharField(max_length=13)

view.py:

objects = Person.objects.defer('phone')
data = serializers.serialize('json', objects, ensure_ascii=False)
return HttpResponse(data, mimetype="application/javascript")

Output:

[{"pk": 1, "model": "ticket.person_deferred_phone", "fields": {}}]

Specifying fields to the serializer like below, gives the same result.

data = serializers.serialize('json', objects, ensure_ascii=False, fields=('name',))

The same issue also applies when using .only().

I would argue that when fields are not specified to the serializer, the entire object should be loaded and serialized.

comment:4 Changed 5 years ago by knutz3n

Summary: Queryset which uses defer() method not serialize in JsonQueryset which uses defer() method not serialize

The bug seems to apply to all serializers, not only JSON.

comment:5 Changed 16 months ago by monikasulik

I tested this on the current master branch and this no longer seems to be a problem.

comment:6 Changed 16 months ago by monikasulik

Resolution: worksforme
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top