Reputation: 640
I have problem with returning something browsable form serializers.serialize
.
My model:
class BowlingGame(models.Model):
Frame = models.PositiveSmallIntegerField()
FrameRow = models.PositiveSmallIntegerField()
Result = models.PositiveSmallIntegerField(blank=True, null=True)
StrikeSpare = models.PositiveSmallIntegerField(blank=True, null=True)
StrikeSpareInfo = models.CharField(max_length=1, blank=True, null=True)
Time = models.DateTimeField(blank=True, null=True)
GameId = models.PositiveIntegerField()
StateOfGame = models.PositiveSmallIntegerField(default=1)
class Meta:
ordering = ('GameId',)
def __str__(self):
return str(self.GameId)
And what I do next:
>>> from django.core import serializers
>>> from django.db.models import Max
>>> from game.models import BowlingGame
>>> a = BowlingGame.objects.all().aggregate(Max('GameId'))['GameId__max']
>>> game_frame = BowlingGame.objects.filter(GameId=a)
>>> me = serializers.serialize('json', game_frame, fields=('Frame', 'FrameRow'))
>>> me
'[{"model": "game.bowlinggame", "pk": 2356, "fields": {"Frame": 1, "FrameRow": 1}}, {"model": "game.bowlinggame", "pk": 2357,......}}]'
This seems to be string as
>>> me[0]
'['
and I'm looking for the first element of the queryset.
I tried few more things:
>>> me = serializers.serialize('json', [game_frame, ], fields=('Frame', 'FrameRow'))
AttributeError: 'QuerySet' object has no attribute '_meta'
My question: is it normal to return string? How I can browse the object. In fact I'm using it with AJAX but its the same. json.game_frame[0]
returns '['
. I need to be able to get the elements separately as normal dict or list. What is going on?
Upvotes: 4
Views: 1698
Reputation: 640
I just find a solution.
In my javascript file var content = JSON.parse(json.game_frame)
.
It creates nice browseble objects.
Upvotes: 1