[Answered ]-Django: working with a multiple foreign key model


1) No.

person = Person.objects.get(pk=1)
phones = person.phone_set.all()
addresses = person.address_set.all()

Also read the docs for select_related


1) You should be able to use this to get the person and his phones/addresses, since it is not a ManyToMany relationship:

person = Person.objects.get(id=1)
phones = Phone.objects.filter(person__id=person.id)
addresses = Address.objects.filter(person__id=person.id)

Most important here is that you don’t want to use get() it will throw an error if more than one record is returned. Get() is for getting one single record, filter() is for multiple.

2) I’m not sure here, but you may need to use separate JSON dictionaries for your person, phones, and addresses. I am not familiar with Wad of Stuff, you may want to look at the source code to see what serializers.serialize() is expecting, and which arguments are defining what you get. Sorry, I can’t be of help there.

Leave a comment