最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Django项目中的Q中的xxx__icontains是什么意思

Django crifan 2265浏览 0评论

折腾:

【已解决】给内容管理系统中添加按Topic去筛选

期间,对于代码:

<code>    def list(self, request, *args, **kwargs):
        """
            获取 script list,取 author=request.user。按历史记录 version 最新的一个script,
            组成列表
        """
        search = request.query_params.get('search', '')
        publish_status = request.query_params.get('publish_status', '')
        #/api/v1/scripts/?topic=Animal&amp;second_level_topic=farm%20animal&amp;publish_status=1
        topic = request.query_params.get('topic', '')
        second_level_topic = request.query_params.get('second_level_topic', '')
        logger.info("search=%s,publish_status=%s,topic=%s,second_level_topic=%s",
            search, publish_status, topic, second_level_topic)
        filter_condition = Q()
        if search:
            filter_condition = filter_condition | Q(place__icontains=search) | Q(title__icontains=search)

        if publish_status:
            if publish_status != '3':
                filter_condition = filter_condition | Q(publish_status=publish_status)

        current_user_script = Script.objects.filter(author=request.user)
</code>

但是先要去搞清楚:

<code>        if search:
            filter_condition = filter_condition | Q(place__icontains=search) | Q(title__icontains=search)

        if publish_status:
            if publish_status != '3':
                filter_condition = filter_condition | Q(publish_status=publish_status)
</code>

Q(publish_status=publish_status)

相对容易理解,就是query中加上对应字段的内容

但是

place__icontains

是什么意思和写法

项目源码中没有搜到类似的东西

django Q icontains

Making queries | Django documentation | Django

https://docs.djangoproject.com/zh-hans/2.0/topics/db/queries/#retrieving-objects

Making queries | Django documentation | Django

“Field lookups¶

Field lookups are how you specify the meat of an SQL WHERE clause. They’re specified as keyword arguments to the QuerySet methods filter(), exclude() and get().

Basic lookups keyword arguments take the form field__lookuptype=value. (That’s a double-underscore). For example:

>>> Entry.objects.filter(pub_date__lte=’2006-01-01′)

translates (roughly) into the following SQL:

SELECT * FROM blog_entry WHERE pub_date <= ‘2006-01-01’;”

https://docs.djangoproject.com/zh-hans/2.0/ref/models/querysets/#std:fieldlookup-icontains

“icontains¶

Case-insensitive containment test.

Example:

Entry.objects.get(headline__icontains=’Lennon’)

SQL equivalent:

SELECT … WHERE headline ILIKE ‘%Lennon%’;”

【总结】

明白了,原来是Django中自带的QuerySet中的filter的写法

此处的:

xxx__icontains=yyy

就是:字段xxx的Case-insensitive的包含yyy的意思

比如:

place__icontains=”Home”

等价于:

<code>SELECT ... WHERE place ILIKE '%Home%';
</code>

官网解释:

QuerySet API reference | Django documentation | Django

中的:

icontains

https://docs.djangoproject.com/zh-hans/2.0/ref/models/querysets/#icontains

转载请注明:在路上 » 【已解决】Django项目中的Q中的xxx__icontains是什么意思

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.180 seconds, using 22.13MB memory