user3036749
user3036749

Reputation: 95

Multiple array to json error django

'list' object has no attribute '_meta'i try to merge 2 object in array after i can this but i can't return json response

def regions(request):
    result_set  = []
    for u in Regions.objects.all()[:100]:
        if 'a' in u.country:
            result_set.append([u ,Subregions.objects.filter(region_id=u.id)])
    data = serializers.serialize('json', result_set)
    return HttpResponse(data)

error code: AttributeError at / 'list' object has no attribute '_meta'

Upvotes: 1

Views: 172

Answers (2)

user2195058
user2195058

Reputation: 927

Basically as its a list of models, it is not able to serialize. And most probably your model has a foreign key relation too.

To solve this you can have a method in the model called as_json(). Which converts the model into a dictionary.

Overall your main target should be to convert the model into dictionary.

Upvotes: 0

falsetru
falsetru

Reputation: 368944

serializers.serialize accepts an iterable that yields model isntances as the second parameter.

But the result_set is a list of list of models. You need to adjust the code to yield model instances.

Upvotes: 1

Related Questions