Django complex query which may need union
I try to do a complex query in Django through these models.
class JobTitleStatus():
PENDING = 0
CONFIRMED = 1
BANNED
class Employer(models.Model):
name = models.CharField(max_length=1000)
isValidated = models.BooleanField(null=False)
eminence = models.IntegerField(blank=False,nu开发者_开发百科ll=False,default=4)
class JobTitle(models.Model)
name = models.CharField(max_length=1000)
employer = models.ForeignKey(Employer,null=True,blank=True)
status = models.IntegerField(null=False, choices=JobTitleStatus)
I try to list all validated employers depending on size of their confirmed
jobtitles.
If there is no Conmfirmed
jobtitle of an employer it should be at end of the list.
I try to do
eList = Employer.objects.filter(name__icontains=emp).filter(isValidated=True)
eList.filter(jobtitle__status=JobTitleStatus.CONFIRMED).annotate(jtt_count=Count('jobtitle')).order_by('-jtt_count','eminence')
This query does what I want more or less however, as you expect, employers which doesn't have Confirmed
job titles are eliminated.
How can I add those Employers
at the end of that query efficiently ?
Thanks
eList.annotate(jtt_count=Count('jobtitle')).order_by('jobtitle__status','-jtt_count','eminence')
should work, I think.
精彩评论