Django 2.1: How to get a list of objects that in each object, contains the objects that have a foreign key...
up vote
0
down vote
favorite
I have these two classes:
Class A:
data
Class B:
a = models.ForeignKey(A)
How can I get an array of A
in which each A
contains the B
's related to them?
I need it because I must return the two tables joined in a JSON.
python django
New contributor
add a comment |
up vote
0
down vote
favorite
I have these two classes:
Class A:
data
Class B:
a = models.ForeignKey(A)
How can I get an array of A
in which each A
contains the B
's related to them?
I need it because I must return the two tables joined in a JSON.
python django
New contributor
With the above modeling, anA
can have zero, one, or moreB
s related toA
.
– Willem Van Onsem
Nov 17 at 15:10
None of your classes is a Django model.
– Klaus D.
Nov 17 at 15:19
Because it's pseudocode
– Jayem97
Nov 17 at 15:23
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have these two classes:
Class A:
data
Class B:
a = models.ForeignKey(A)
How can I get an array of A
in which each A
contains the B
's related to them?
I need it because I must return the two tables joined in a JSON.
python django
New contributor
I have these two classes:
Class A:
data
Class B:
a = models.ForeignKey(A)
How can I get an array of A
in which each A
contains the B
's related to them?
I need it because I must return the two tables joined in a JSON.
python django
python django
New contributor
New contributor
edited Nov 17 at 19:36
user6910411
31.8k76692
31.8k76692
New contributor
asked Nov 17 at 15:09
Jayem97
32
32
New contributor
New contributor
With the above modeling, anA
can have zero, one, or moreB
s related toA
.
– Willem Van Onsem
Nov 17 at 15:10
None of your classes is a Django model.
– Klaus D.
Nov 17 at 15:19
Because it's pseudocode
– Jayem97
Nov 17 at 15:23
add a comment |
With the above modeling, anA
can have zero, one, or moreB
s related toA
.
– Willem Van Onsem
Nov 17 at 15:10
None of your classes is a Django model.
– Klaus D.
Nov 17 at 15:19
Because it's pseudocode
– Jayem97
Nov 17 at 15:23
With the above modeling, an
A
can have zero, one, or more B
s related to A
.– Willem Van Onsem
Nov 17 at 15:10
With the above modeling, an
A
can have zero, one, or more B
s related to A
.– Willem Van Onsem
Nov 17 at 15:10
None of your classes is a Django model.
– Klaus D.
Nov 17 at 15:19
None of your classes is a Django model.
– Klaus D.
Nov 17 at 15:19
Because it's pseudocode
– Jayem97
Nov 17 at 15:23
Because it's pseudocode
– Jayem97
Nov 17 at 15:23
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
First set the back relation of a
and then migrate the database:
Class A(models.Model):
data
Class B(models.Model):
a = models.ForeignKey(A, related_name='b_relations', null=False, blank=False, on_delete=models.CASCADE)
Now you can access the back relation. You need to call .all()
on b_relations
because it is lazy loaded.
A.objects.first().b_relations.all()
To get json data I suggest to use django rest framework's ModelSerializer:
class BSerializer(serializers.ModelSerializer):
class Meta:
model = B
fields = (
'pk', # primary key
'a',
)
depth = 0
class ASerializer(serializers.ModelSerializer):
b_relations = BSerializer(many=True, read_only=True))
class Meta:
model = A
fields = (
'pk', # primary key
'b_relations',
)
depth = 0
To get json data:
a_relations: QuerySet = A.objects.all()
serializer = ASerializer(a_relations, many=True)
json_data: Dict = serializer.data
In case you've never used Django Rest Framework: Django Rest Framework - Getting Started
Have fun!
1
Thank you very much, it's been very helpful and now it works!
– Jayem97
Nov 17 at 16:00
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
First set the back relation of a
and then migrate the database:
Class A(models.Model):
data
Class B(models.Model):
a = models.ForeignKey(A, related_name='b_relations', null=False, blank=False, on_delete=models.CASCADE)
Now you can access the back relation. You need to call .all()
on b_relations
because it is lazy loaded.
A.objects.first().b_relations.all()
To get json data I suggest to use django rest framework's ModelSerializer:
class BSerializer(serializers.ModelSerializer):
class Meta:
model = B
fields = (
'pk', # primary key
'a',
)
depth = 0
class ASerializer(serializers.ModelSerializer):
b_relations = BSerializer(many=True, read_only=True))
class Meta:
model = A
fields = (
'pk', # primary key
'b_relations',
)
depth = 0
To get json data:
a_relations: QuerySet = A.objects.all()
serializer = ASerializer(a_relations, many=True)
json_data: Dict = serializer.data
In case you've never used Django Rest Framework: Django Rest Framework - Getting Started
Have fun!
1
Thank you very much, it's been very helpful and now it works!
– Jayem97
Nov 17 at 16:00
add a comment |
up vote
0
down vote
accepted
First set the back relation of a
and then migrate the database:
Class A(models.Model):
data
Class B(models.Model):
a = models.ForeignKey(A, related_name='b_relations', null=False, blank=False, on_delete=models.CASCADE)
Now you can access the back relation. You need to call .all()
on b_relations
because it is lazy loaded.
A.objects.first().b_relations.all()
To get json data I suggest to use django rest framework's ModelSerializer:
class BSerializer(serializers.ModelSerializer):
class Meta:
model = B
fields = (
'pk', # primary key
'a',
)
depth = 0
class ASerializer(serializers.ModelSerializer):
b_relations = BSerializer(many=True, read_only=True))
class Meta:
model = A
fields = (
'pk', # primary key
'b_relations',
)
depth = 0
To get json data:
a_relations: QuerySet = A.objects.all()
serializer = ASerializer(a_relations, many=True)
json_data: Dict = serializer.data
In case you've never used Django Rest Framework: Django Rest Framework - Getting Started
Have fun!
1
Thank you very much, it's been very helpful and now it works!
– Jayem97
Nov 17 at 16:00
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
First set the back relation of a
and then migrate the database:
Class A(models.Model):
data
Class B(models.Model):
a = models.ForeignKey(A, related_name='b_relations', null=False, blank=False, on_delete=models.CASCADE)
Now you can access the back relation. You need to call .all()
on b_relations
because it is lazy loaded.
A.objects.first().b_relations.all()
To get json data I suggest to use django rest framework's ModelSerializer:
class BSerializer(serializers.ModelSerializer):
class Meta:
model = B
fields = (
'pk', # primary key
'a',
)
depth = 0
class ASerializer(serializers.ModelSerializer):
b_relations = BSerializer(many=True, read_only=True))
class Meta:
model = A
fields = (
'pk', # primary key
'b_relations',
)
depth = 0
To get json data:
a_relations: QuerySet = A.objects.all()
serializer = ASerializer(a_relations, many=True)
json_data: Dict = serializer.data
In case you've never used Django Rest Framework: Django Rest Framework - Getting Started
Have fun!
First set the back relation of a
and then migrate the database:
Class A(models.Model):
data
Class B(models.Model):
a = models.ForeignKey(A, related_name='b_relations', null=False, blank=False, on_delete=models.CASCADE)
Now you can access the back relation. You need to call .all()
on b_relations
because it is lazy loaded.
A.objects.first().b_relations.all()
To get json data I suggest to use django rest framework's ModelSerializer:
class BSerializer(serializers.ModelSerializer):
class Meta:
model = B
fields = (
'pk', # primary key
'a',
)
depth = 0
class ASerializer(serializers.ModelSerializer):
b_relations = BSerializer(many=True, read_only=True))
class Meta:
model = A
fields = (
'pk', # primary key
'b_relations',
)
depth = 0
To get json data:
a_relations: QuerySet = A.objects.all()
serializer = ASerializer(a_relations, many=True)
json_data: Dict = serializer.data
In case you've never used Django Rest Framework: Django Rest Framework - Getting Started
Have fun!
answered Nov 17 at 15:30
Tobias Ernst
633510
633510
1
Thank you very much, it's been very helpful and now it works!
– Jayem97
Nov 17 at 16:00
add a comment |
1
Thank you very much, it's been very helpful and now it works!
– Jayem97
Nov 17 at 16:00
1
1
Thank you very much, it's been very helpful and now it works!
– Jayem97
Nov 17 at 16:00
Thank you very much, it's been very helpful and now it works!
– Jayem97
Nov 17 at 16:00
add a comment |
Jayem97 is a new contributor. Be nice, and check out our Code of Conduct.
Jayem97 is a new contributor. Be nice, and check out our Code of Conduct.
Jayem97 is a new contributor. Be nice, and check out our Code of Conduct.
Jayem97 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352492%2fdjango-2-1-how-to-get-a-list-of-objects-that-in-each-object-contains-the-objec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
With the above modeling, an
A
can have zero, one, or moreB
s related toA
.– Willem Van Onsem
Nov 17 at 15:10
None of your classes is a Django model.
– Klaus D.
Nov 17 at 15:19
Because it's pseudocode
– Jayem97
Nov 17 at 15:23