我有以下型號:Django的LEFT JOIN - 一對多
class Employee(models.Model):
def getFullname(self):
return '{}, {} {}'.format(self.empLastname,self.empFirstname, self.empMiddlename)
empID = models.CharField(primary_key=True, max_length= 50, null=False)
empLastname = models.CharField(max_length=50)
empFirstname = models.CharField(max_length=50)
empMiddlename = models.CharField(max_length=50, null=True)
fullname = property(getFullname)
def __str__(self):
return self.fullname
class Picture(models.Model):
empID = models.ForeignKey(Employee, related_name="pictures", to_field='empID', on_delete = models.CASCADE)
picture = models.ImageField('Uploaded image')
isDef = models.BooleanField(default=False)
我想查詢所有員工中的圖片模型isDefault=True
。但並非所有員工都有照片。所以基本上我想查詢所有員工哪些isDefault=True
和所有沒有圖片的員工。
我的查詢是這樣的:
SELECT manager_employee.`*`, manager_picture.*
FROM manager_employee
LEFT JOIN manager_picture ON (manager_employee.empID = manager_picture.empID_id
AND manager_picture.isDef)
serializer.py
class PictureSerializer(serializers.ModelSerializer):
class Meta:
model = Picture
fields = (
'empID',
'picture',
'isDef'
)
class EmployeeListSerializer(serializers.ModelSerializer):
pictures = PictureSerializer(many = True, read_only = True)
class Meta:
model = Employee
fields = (
'empID',
'empLastname',
'empFirstname',
'empMiddlename',
'fullname',
'pictures'
)
該查詢給了我正確的結果。該問題是如何做到這一點的Django
..
假設我有員工記錄模型
============================================================================= empID | empLastname | empFirstname | empMiddlename | fullname | ============================================================================= 1 | Doe | John | Smith | Doe, John Smith | ----------------------------------------------------------------------------- 2 | Potter | Harry | Moon | Potter, Harry ..| ----------------------------------------------------------------------------- 3 | Aaaa | Abbb | Accc | ... | ----------------------------------------------------------------------------- 4 | Baaaa | Bbbb | Bccc | ... | -----------------------------------------------------------------------------
現在在圖片模式
================================================ empID | Picture | isDef | ================================================ 1 | Pic1.jpg | 0 | ------------------------------------------------ 1 | Pic2.jpg | 0 | ------------------------------------------------ 1 | Pic3.jpg | 1 | *which makes this as default* ------------------------------------------------ 2 | emp2Pic1.jpg | 1 | *which makes this as default* ------------------------------------------------ 2 | emp2Pic2.jpg | 0 | ------------------------------------------------ 4 | emp4Pic1.jpg | 0 | ------------------------------------------------ 4 | emp4Pic2.jpg | 1 | *which makes this as default* ------------------------------------------------
現在,當我嘗試這個SQL:
SELECT manager_employee.`*`, manager_picture.*
FROM manager_employee
LEFT JOIN manager_picture ON (manager_employee.empID = manager_picture.empID_id
AND manager_picture.isDefault)
這給我一個結果:
========================================================================= empID | empLastname | empFirstname | empMiddle | Picture | isDef | 1 | Doe | John | Smith | Pic2.jpg | 1 | 2 | Potter | Harry | Moon | emp2Pic1.jpg | 1 | 3 | Aaaa | Abbb | Accc | (null) | (null) | 4 | Baaaa | Bbbb | Bccc | emp4Pic2.jpg | 1 | -------------------------------------------------------------------------
如何我在Django實現這一點?
請別人幫我.. TIA ..
檢查這個http://stackoverflow.com/questions/21271835/left-join-django-orm –
@ PiyushS.Wanare我檢查的鏈接。但答案是LEFT OUTER JOIN而不是LEFT JOIN。當我測試答案,它沒有給我什麼.. – N0LrAm
'Picture.objects.filter(isDefault = True,picture__isnull = True)'這是你要找的嗎?這將爲您提供來自Picture模型的所有員工,其中isDefault = True且沒有圖片。 –