I have two models such that
class Employer(models.Model):
code = models.CharField(null=False,blank=False,default="")
class JobTitle(models.Model):
employer = models.ForeignKey(Employer,unique=False,null=False,default=0)
name = models.CharField(max_length=1000,null=False,default="")
开发者_如何学编程
and I would like to get all employers whose jobtitle name is less than X chracters. How can I achive this in Django ?
Thanks
Use extra
:
Emp.objects.extra(select={'name_length': 'LENGTH(`name`)'},
where=['name_length < 24'])
or raw
:
Emp.objects.raw('SELECT * FROM myapp_emp WHERE LENGTH(`name`) < 24')
精彩评论