本文介绍了Django中的动态逻辑查询构建器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在数据库中有2个表:
I'm have 2 table in DB:
class Param(models.Model):
s_name = models.CharField(max_length=200)
n_name = models.CharField(max_length=200)
class ParamValue(models.Model):
param = models.ForeignKey(Param)
value = models.IntegerField()
created = models.DateTimeField(default=datetime.now, blank=True)
我想创建动态构造函数.是否有用于创建动态逻辑过滤器的库或方法,例如Apache Lucene或Solr?我的意思是这样的:
I wanted to create dynamic constructor. Is there any library or method for create dynamic logic filter like as Apache Lucene or Solr?I mean something like this:
dyn_filter = parse("(value < 200 AND value__s_name == 'pressure')
OR (value > 10 AND value__s_name == 'depth')")
result = ParamValue.objects.filter(dyn_filer)
推荐答案
检查库 DjangoQL .它支持逻辑运算符,比较运算符,括号,表联接等.
Check the library DjangoQL. It supports logical operators, comparison operators, parenthesis, table joins, etc.
from django.db import models
from djangoql.queryset import DjangoQLQuerySet
class Book(models.Model):
name = models.CharField(max_length=255)
author = models.ForeignKey('auth.User')
objects = DjangoQLQuerySet.as_manager()
在上面的示例中,您可以像这样执行搜索:
With the example above you can perform search like this:
qs = Book.objects.djangoql(
'name ~ "war" and author.last_name = "Tolstoy"'
)
然后DjangoQL将执行等效的Django查询集.
And DjangoQL will execute the equivalent Django queryset.
这篇关于Django中的动态逻辑查询构建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!