0

This is the model I use:

from django.db import models

class A(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)

class B models.Model):
    name = models.CharField(max_length=100)
    author = models.ForeignKey(A)

class C models.Model):
    name = models.CharField(max_length=100)
    author = models.ForeignKey(A)

class D models.Model):
    name = models.CharField(max_length=100)
    author = models.ForeignKey(A)

I want to select one A object and all its related B, C, D objects to serialize in json. Serializing is fine but how could I select A and related objects?

1 Answer 1

1

Use the .FOO_set-manager:

var a = A.objects.get(id)
var b = a.b_set.all() # All related B objects to a
var c = a.c_set.all() # All related C objects to a
...etc
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.