5

I am setting webservices for an application and I have the following models:

class Parent(models.Model):
    ...
class Child(models.Model):
    parent = models.ForeignKey(Course)
    ...

The relation is One to Many (1 Parent, many Child) Now, I would like to get all the Parent objects with its particular Child and send it as JSON Request. Is it possible to do so without having to first get all the "Childs" and iterate them looking for the ones related to the particular parent? I think that would be extremely inefficient for really large databases, plus the "Childs" won't be repeated in other "Parents"

Thank you very much

Alqueraf
  • 1,048
  • 11
  • 25

3 Answers3

17

Every relationship in Django automatically gets its reverse relation added to the model. In the case of a ForeignKey or ManyToManyField that relation contains several objects. In that case, the default attribute name is set to <model>_set, so in this case child_set. This is a manager and can be used as such, so e.g. to iterate over all children:

for child in parent.child_set.all():
    do_something()

You can also specify the attribute name used for the reverse relation using the related_name attribute:

class Child(models.Model):
    parent = models.ForeignKey(Parent, related_name='children')

for child in parent.children.filter(some_field=True):
    do_something()

Read more in the documentation on following relations backwards and how are backward relationships possible.

knbk
  • 49,685
  • 7
  • 113
  • 113
2

Why would you need to iterate? Even if Django didn't provide you with a special backwards syntax, you could always do this:

Child.objects.filter(parent=my_parent)

but as a cursory Google for the title of your question would have shown, there is a special syntax for backwards relations:

my_parent.child_set.all()
Daniel Roseman
  • 567,968
  • 59
  • 825
  • 842
0

Yes, in django you can use:

parentInstance.child_set.all()

where parentInstance is one particular parent in your Parent database. That will return all of the Child objects associated with it in an efficient manner. To make it a JSON response, you can try something like this:

import json

from django.http import HttpResponse

response_data = {}
response_data[str(parentInstance)] = parentInstance.child_set.all()
return HttpResponse(json.dumps(response_data), content_type="application/json"

adopted from here.

Community
  • 1
  • 1
rofls
  • 4,795
  • 3
  • 25
  • 37