1

I'm trying to join 2 entities, get specific fields from them, and return a JSON of that.
I tried writing the following code:

import datetime
result = Foo.objects.all()
result = result.select_related('bar').extra(select={'bar_has_address':'IF(bar.has_address = '',0,1)'})
result = result.filter(time__gte=datetime.date.today())
return HttpResponse(serializers.serialize('json', result),mimetype="application/json")

Now I'm only getting a json containing the fields of Foo, whereas I want to get Bar's fields as well, ideally the returned JSON would have specific fields from both entities:

[{
    'name': 'lorem ipsum', //from Foo
    'has_address': 1, //from Bar
    'address': 'some address', //from Bar
    'id': 1, //from Foo
},... ]

even under result.values('...') I'm not getting any of Bar's fields
What am I missing here?

Asaf
  • 8,106
  • 19
  • 66
  • 116

1 Answers1

1

As far as I know, django built-in serializers cannot work with model related fields. Take a look at:

Also see:

Hope that helps.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I'll give Fullserializers a try, see if it solves my problem, it's not really intuitive if by default it ignores the select_related & extra – Asaf Jun 12 '13 at 15:14
  • @Asaf did you give serializers a try? :) Should I remove the answer or you can mark it as accepted to resolve the topic? Thanks. – alecxe Jan 24 '16 at 06:00