Django模型
class Imones(models.Model): # Clients
id = models.AutoField(primary_key=True)
pavadinimas = models.CharField(unique=True, max_length=45) # name
class Priekabos(models.Model): # Products
id = models.AutoField(primary_key=True)
sutarties_nr = models.CharField(unique=True, max_length=45) # contract_number
vin = models.CharField(unique=True, max_length=45, verbose_name='VIN') # vehicle_identification_number
imones = models.ForeignKey(Imones, models.DO_NOTHING, verbose_name='Įmonė') # client_id
class Saskaitos(models.Model): # Invoices
id = models.AutoField(primary_key=True)
apmoketa = models.BooleanField(default=0) # Paid in full
imones = models.ForeignKey(Imones, models.DO_NOTHING) # client_id
MySQL查询:
SELECT distinct sutarties_nr, vin, apmoketa
FROM priekabos pr
LEFT OUTER JOIN saskaitos sa
ON pr.imones_id = sa.imones_id
WHERE apmoketa=0 or apmoketa is null
有人可以解释如何将MySQL查询翻译成Django查询吗?我试过了:
obj = Saskaitos.objects.filter(apmoketa=0)
queryset = Priekabos.objects.filter(imones__in=obj.values('imones')).values()
但它没有按预期返回所有值。还有一个问题 - 如何使Imones.pavadinimas出现在queryset结果而不是普通id?现在看起来like that