1

'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'

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

after this; <Subregions: Subregions object> is not JSON serializable result_set = [] for u in Regions.objects.all()[:100]: if 'a' in u.country: result_set.append([u.country, list(Subregions.objects.filter(region_id=u.id))]) data = json.dumps(result_set) return HttpResponse(data, mimetype='application/json')
i get same error that is is not JSON serializable. Same ruby code doesn't get error code that @result_set.push([c.country,City.where(region_id:c.id).take(20)])
@user3036749, how about this? result_set.append(u); result_set.extend(Subregions.objects.filter(region_id=u.id)). edited the answer accordingly.
result_set.append(u)TypeError at / <Regions: Regions object> is not JSON serializable
@user3036749, Could you show me the full traceback by uploading it somewhere (like pastebin)
|
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.