在下面的类中,我需要显示姓名长度大于25且国家=UK
class Emp(models.Model):
name=models.CharField(max_length=2000)
country=models.CharField(max_length=1000)
我试过了,但是不工作
from django.db.models import CharField
from django.db.models.functions import Length
CharField. register_lookup(Length, 'length')
result = Emp.objects.filter(country='UK','name__length__gte=10)
你可以试试;
from django.db.models.functions import Length
result = Emp.objects.annotate(name_length=Length("name")).filter(country='UK', name_length__gte=25)
看文档
希望对您有所帮助,下面是代码片段
from django.db.models.functions import Length
from django.db.models import F
result = YOUR_MODEL.objects.annotate(column_length=Length(F('Your_column'))).filter(column_length__lt=100)
rating=result.all()