how can i make this queryset:
JavaScript
x
8
1
SELECT
2
p.*,
3
o.name,
4
o.link
5
FROM property p
6
INNER JOIN listing l on l.property_id = p.id
7
INNER JOIN ota o on o.id = l.ota_id
8
models
JavaScript
1
12
12
1
class Property(Model):
2
code = CharField()
3
4
5
class Listing(Model):
6
ota = ForeignKey(Ota)
7
property = ForeignKey(Property)
8
9
class Ota(Model):
10
name = Charfield()
11
link = Charfield()
12
with Property.objects.all()
I can see in the returned object that there is a listing_set
let say:
JavaScript
1
3
1
queryset = Property.objects.all()
2
queryset[0].listing_set.all()
3
but it brings the entire model, and not the related to property_id;
also tried to get Ota data in serializer with SerializerMethodField
and perform a new query to get this info, but decrease performance significantly.
Advertisement
Answer
I think you should use related_name
JavaScript
1
12
12
1
class Property(Model):
2
code = CharField()
3
4
5
class Listing(Model):
6
ota = ForeignKey(Ota)
7
property = ForeignKey(Property,related_name='listinings')
8
9
class Ota(Model):
10
name = Charfield()
11
link = Charfield()
12
and
JavaScript
1
3
1
pp=Property.objects.first()
2
pp.listinings.all()
3