zoukankan      html  css  js  c++  java
  • Django – query not equal

    The simpliest way to retrieve data from tables is take them all. To do this,  you can write:

    1
    all_entries = Entry.objects.all()

    But, usually, you have to select a subset of data. To reach this goal, you can filter the QuerySet with some conditions. The fastest way is yo use the .filter() method, giving as parameter our filterting conditions. For example:

    1
    Entry.objects.filter(date = 2006)

    And.. how we can make a not equal filtering condtion?
    Changing ‘=’ with ‘!=’ or ‘<>’,  will return error messages. And now? The solution is simple.
    First of all, import in out file the library for the Q object:

    1
    from django.db.models import Q

    Then, we can include our condition in a Q object. To make this a not equal query, write ‘~’ just before the Q object.

    1
    Entry.objects.filter(~Q(date = 2006))

    In this case, the code will return all entries with date field different from 2006.

  • 相关阅读:
    hdu 1260 Tickets
    hdu 4738 Caocao's Bridges(桥的最小权值+去重)
    找规律
    C语言快速排序
    数组的初始化方法
    C语言选择排序
    副本机制
    安装完Kali的后续操作
    Bool盲注
    Python中的列表
  • 原文地址:https://www.cnblogs.com/ymy124/p/5466844.html
Copyright © 2011-2022 走看看