开发者

django filter depeding on character count of the item

开发者 https://www.devze.com 2023-04-08 02:38 出处:网络
I have two models such that class Employer(models.Model): code = models.CharField(null=False,blank=False,default=\"\")

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')
0

精彩评论

暂无评论...
验证码 换一张
取 消