How to perform inner join of 1 table with 2 other tables in Django?
I have this Django model example
class Users(models.Model):
first_name = models.CharField(max_length=255, blank=True, null=True,unique=True)
last_name = models.CharField(max_length=255, blank=True, null=True)
class TableA(models.Model):
user= models.ForeignKey(Users, on_delete=models.CASCADE)
atrribute_a_1 = models.CharField(max_length=255, blank=True, null=True,unique=True)
atrribute_a_2 = models.CharField(max_length=255, blank=True, null=True)
class TableB(models.Model):
user= models.ForeignKey(Users, on_delete=models.CASCADE)
table_a_id= models.ForeignKey(TableA, on_delete=models.CASCADE)
atrribute_b_1 = models.DecimalField(max_digits = 100, decimal_places = 8, default=0)
atrribute_b_2 = models.DecimalField(max_digits = 100, decimal_places = 8, default=0)
atrribute_b_3 = models.DecimalField(max_digits = 100, decimal_places = 8, default=0)
class TableC(models.Model):
user= models.ForeignKey(Users, on_delete=models.CASCADE)
table_a_id= models.ForeignKey(TableA, on_delete=models.CASCADE)
atrribute_c_1 = models.DecimalField(max_digits = 100, decimal_places = 8, default=0)
atrribute_c_2 = models.DecimalField(max_digits = 100, decimal_places = 8, default=0)
class TableASerializer(serializers.ModelSerializer):
class Meta:
model = TableA
fields = '__all__'
class TableBSerializer(serializers.ModelSerializer):
class Meta:
model = TableB
fields = '__all__'
class TableCSerializer(serializers.ModelSerializer):
class Meta:
model = TableC
fields = '__all__'
I want to perform this action:
select * from TableA inner join TableB on TableA.id=TableB.table_a_id inner join TableC on TableA.id=TableC.table_a_id where TableA.id=3
How am I supposed to do that? as I tried filter and select_related and it didn't work as I checked in the internet or even documentation examamples they start the selection from TableC or TableB , but in my case I want to start from TableA Like:
my_query=TableA.Objects.filter(id=3)....something
serializer=TableASerializer(my_query,many=False).data
In results I want [All TableA attributes + All TableB attributes + All TableC attributes]