Django Внутреннее и левое присоединение
Я пытаюсь сопоставить SQL с Django ORM, но безуспешно. Я хочу сопоставить приведенный ниже SQL-запрос с Django. Можете ли вы мне помочь?
SELECT DCon.Name, Dcon.Address1,
FROM Fact FPT
JOIN Profile DPP ON DPP.ProfileKey = FPT.ProfileKey
JOIN Consignee DCon ON DCon.ConsigneeKey = FPT.ConsigneeKey
LEFT Join Country DC ON DC.CountryID = DCon.CountryID and DC.IsCurrent = 'Y'
WHERE DPP.trackingCode = SomeTrackingCode AND Dpp.Number = SomeNumber
Вот что я пробовал до сих пор
data = Fact.objects.select_related("profilekey", "consigneekey")
class Fact(models.Model):
profilekey = models.ForeignKey(
"Profile", on_delete=models.PROTECT, db_column="profilekey"
)
consigneekey = models.ForeignKey(
"Consignee", on_delete=models.PROTECT, db_column="consigneekey"
)
countrykey = models.ForeignKey(
"Country", on_delete=models.PROTECT, db_column="countryid"
)
dateordered = models.DateTimeField()
dateprocessed = models.DateTimeField()
class Country(models.Model):
countryid = models.BigIntegerField()
countryname = models.CharField(max_length=64)
iscurrent = models.CharField(max_length=1)
class Consignee(models.Model):
Consigneekey = models.BigIntegerField(primary_key=True)
name = models.CharField(max_length=500)
countryid = models.BigIntegerField()
class Profile(models.Model):
dimparcelprofilekey = models.BigIntegerField(primary_key=True)
number = models.CharField(max_length=50)
trackingcode = models.CharField(max_length=64)