N
N
neoliz2015-12-01 23:11:54
JavaScript
neoliz, 2015-12-01 23:11:54

Python + json, why is the array not being serialized?

Tell me please. Why doesn't it serialize a list of Tiket objects for me???
I want to send an array to the page on an ajax request and change the data there. Gets the list normally. The problem is wrapping in json
Here is the code:

def opens(request):
    t = Tiket.objects.filter(user=request.user, sost=True)
   
    return JsonResponse({'tiketsS':t})

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Artem Klimenko, 2015-12-02
@timofeydeys

t = Tiket.objects.filter(user=request.user, sost=True)

this is not a list (list) of objects, this is a lazy QuerySet, which, when trying to iterate over it, will be calculated (and in a number of other cases), and at each step will return model instances, and this object cannot be converted to json (by standard means) .
At the same time, model instances are not converted to json in any way, there are no models in json, if you want to get a list of json objects, then you need to submit a list of dictionaries.
t = Tiket.objects.filter(user=request.user, sost=True).values()
   
    return JsonResponse({'tiketsS': list(t)})

At the same time, you need to be sure that all the fields that we feed the JsonResponse Jang can convert to json (your model should be able to), just pay attention to the date, it will become a string for you.
Z.Y. if you want to reply, use "comment"

A
Alexey Cheremisin, 2015-12-01
@leahch

Most likely your objects have non-serializable types. Try to convert via json.dumps(t) and catch the exception.

N
Neoliz, 2015-12-01
@timofeydeys

Most likely your objects have non-serializable types. Try to convert via json.dumps(t) and catch the exception.

Here is the model:
class Tiket(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=350)
    info = models.TextField()
    sost = models.BooleanField(default=True)  # True - открыт тикет
    date = models.DateTimeField('Дата создания')
    manager = models.ForeignKey(Manager_cab)  # Прикреплен ли менеджер
    new_mess = models.BooleanField(default=False)
    kol_mess = models.IntegerField(default=0)

dumps does not serialize. It gives the same.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question