我有一张桌子/模型' ABC'它有两个列字段' A'和' B'。我想查询上面的表格如下:
select A, B from ABC where B is not null;
我能够做到
select A, B from ABC;
但我不知道如何引入空检查条件。 我尝试过以下方面没有成功:
ABC.objects.values('A', 'B') # which is getting all the tuples including null valued 'B' column
ABC.objects.values('A', 'B', 'B__isnull=False') # which is incorrect
ABC.objects.values('A', 'B__isnull=False') # which is incorrect again.
有人可以提供我可以使用的正确格式/查询吗?
感谢。
答案 0 :(得分:2)
没关系,我明白了:)
configure:2270: checking for a BSD-compatible install
configure:2338: result: /usr/bin/install -c
configure:2349: checking whether build environment is sane
configure:2399: result: yes
configure:2540: checking for a thread-safe mkdir -p
configure:2579: result: ./install-sh -c -d
configure:2592: checking for gawk
configure:2622: result: no
configure:2592: checking for mawk
configure:2622: result: no
configure:2592: checking for nawk
configure:2622: result: no
configure:2592: checking for awk
configure:2608: found /usr/bin/awk
configure:2619: result: awk
configure:2630: checking whether make sets $(MAKE)
configure:2652: result: yes
configure:2785: checking for g++
configure:2801: found /usr/bin/g++
configure:2812: result: g++
configure:2839: checking for C++ compiler version
configure:2848: g++ --version >&5
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
configure:2859: $? = 0
configure:2848: g++ -v >&5
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
configure:2859: $? = 0
configure:2848: g++ -V >&5
clang: error: argument to '-V' is missing (expected 1 value)
clang: error: no input files
configure:2859: $? = 1
configure:2848: g++ -qversion >&5
clang: error: unknown argument: '-qversion'
clang: error: no input files
configure:2859: $? = 1
configure:2879: checking whether the C++ compiler works
configure:2901: g++ conftest.cpp >&5
configure:2905: $? = 0
configure:2953: result: yes
configure:2956: checking for C++ compiler default output file name
configure:2958: result: a.out
configure:2964: checking for suffix of executables
configure:2971: g++ -o conftest conftest.cpp >&5
configure:2975: $? = 0
configure:2997: result:
configure:3019: checking whether we are cross compiling
configure:3027: g++ -o conftest conftest.cpp >&5
configure:3031: $? = 0
configure:3038: ./conftest
configure:3042: $? = 0
configure:3057: result: no
configure:3062: checking for suffix of object files
configure:3084: g++ -c conftest.cpp >&5
configure:3088: $? = 0
configure:3109: result: o
configure:3113: checking whether we are using the GNU C++ compiler
configure:3132: g++ -c conftest.cpp >&5
configure:3132: $? = 0
configure:3141: result: yes
configure:3150: checking whether g++ accepts -g
configure:3170: g++ -c -g conftest.cpp >&5
configure:3170: $? = 0
configure:3211: result: yes
configure:3245: checking for style of include used by make
configure:3273: result: GNU
configure:3299: checking dependency style of g++
configure:3410: result: gcc3
configure:3473: checking for gcc
configure:3489: found /usr/bin/gcc
configure:3500: result: gcc
configure:3729: checking for C compiler version
configure:3738: gcc --version >&5
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
configure:3749: $? = 0
configure:3738: gcc -v >&5
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
configure:3749: $? = 0
configure:3738: gcc -V >&5
clang: error: argument to '-V' is missing (expected 1 value)
clang: error: no input files
configure:3749: $? = 1
configure:3738: gcc -qversion >&5
clang: error: unknown argument: '-qversion'
clang: error: no input files
configure:3749: $? = 1
configure:3753: checking whether we are using the GNU C compiler
configure:3772: gcc -c conftest.c >&5
configure:3772: $? = 0
configure:3781: result: yes
configure:3790: checking whether gcc accepts -g
configure:3810: gcc -c -g conftest.c >&5
configure:3810: $? = 0
configure:3851: result: yes
configure:3868: checking for gcc option to accept ISO C89
configure:3932: gcc -c -g -O2 conftest.c >&5
configure:3932: $? = 0
configure:3945: result: none needed
configure:3967: checking dependency style of gcc
configure:4078: result: gcc3
configure:4141: checking how to run the C preprocessor
configure:4172: gcc -E -I../../deps/local/include -I/ad3 conftest.c
configure:4172: $? = 0
configure:4186: gcc -E -I../../deps/local/include -I/ad3 conftest.c
conftest.c:9:10: fatal error: 'ac_nonexistent.h' file not found
#include <ac_nonexistent.h>
^
1 error generated.
configure:4186: $? = 1
configure: failed program was:
| /* confdefs.h */
| #define PACKAGE_NAME "TurboParser"
| #define PACKAGE_TARNAME "TurboParser"
| #define PACKAGE_VERSION "2.1.0"
| #define PACKAGE_STRING "TurboParser 2.1.0"
| #define PACKAGE_BUGREPORT "afm@cs.cmu.edu"
| #define PACKAGE_URL "http://www.ark.cs.cmu.edu/TurboParser/"
| /* end confdefs.h. */
| #include <ac_nonexistent.h>
configure:4211: result: gcc -E
configure:4231: gcc -E -I../../deps/local/include -I/ad3 conftest.c
configure:4231: $? = 0
configure:4245: gcc -E -I../../deps/local/include -I/ad3 conftest.c
conftest.c:9:10: fatal error: 'ac_nonexistent.h' file not found
#include <ac_nonexistent.h>
^
1 error generated.
configure:4245: $? = 1
configure: failed program was:
| /* confdefs.h */
| #define PACKAGE_NAME "TurboParser"
| #define PACKAGE_TARNAME "TurboParser"
| #define PACKAGE_VERSION "2.1.0"
| #define PACKAGE_STRING "TurboParser 2.1.0"
| #define PACKAGE_BUGREPORT "afm@cs.cmu.edu"
| #define PACKAGE_URL "http://www.ark.cs.cmu.edu/TurboParser/"
| /* end confdefs.h. */
| #include <ac_nonexistent.h>
configure:4274: checking for grep that handles long lines and -e
configure:4332: result: /usr/bin/grep
configure:4337: checking for egrep
configure:4399: result: /usr/bin/grep -E
configure:4404: checking for ANSI C header files
configure:4424: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4424: $? = 0
configure:4497: gcc -o conftest -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4497: $? = 0
configure:4497: ./conftest
configure:4497: $? = 0
configure:4508: result: yes
configure:4521: checking for sys/types.h
configure:4521: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4521: $? = 0
configure:4521: result: yes
configure:4521: checking for sys/stat.h
configure:4521: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4521: $? = 0
configure:4521: result: yes
configure:4521: checking for stdlib.h
configure:4521: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4521: $? = 0
configure:4521: result: yes
configure:4521: checking for string.h
configure:4521: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4521: $? = 0
configure:4521: result: yes
configure:4521: checking for memory.h
configure:4521: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4521: $? = 0
configure:4521: result: yes
configure:4521: checking for strings.h
configure:4521: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4521: $? = 0
configure:4521: result: yes
configure:4521: checking for inttypes.h
configure:4521: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4521: $? = 0
configure:4521: result: yes
configure:4521: checking for stdint.h
configure:4521: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4521: $? = 0
configure:4521: result: yes
configure:4521: checking for unistd.h
configure:4521: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4521: $? = 0
configure:4521: result: yes
configure:4536: checking stddef.h usability
configure:4536: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4536: $? = 0
configure:4536: result: yes
configure:4536: checking stddef.h presence
configure:4536: gcc -E -I../../deps/local/include -I/ad3 conftest.c
configure:4536: $? = 0
configure:4536: result: yes
configure:4536: checking for stddef.h
configure:4536: result: yes
configure:4536: checking for string.h
configure:4536: result: yes
configure:4536: checking sys/time.h usability
configure:4536: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4536: $? = 0
configure:4536: result: yes
configure:4536: checking sys/time.h presence
configure:4536: gcc -E -I../../deps/local/include -I/ad3 conftest.c
configure:4536: $? = 0
configure:4536: result: yes
configure:4536: checking for sys/time.h
configure:4536: result: yes
configure:4548: checking for stdbool.h that conforms to C99
configure:4615: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4615: $? = 0
configure:4622: result: yes
configure:4624: checking for _Bool
configure:4624: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4624: $? = 0
configure:4624: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
conftest.c:58:20: error: expected expression
if (sizeof ((_Bool)))
^
1 error generated.
configure:4624: $? = 1
configure: failed program was:
| /* confdefs.h */
| #define PACKAGE_NAME "TurboParser"
| #define PACKAGE_TARNAME "TurboParser"
| #define PACKAGE_VERSION "2.1.0"
| #define PACKAGE_STRING "TurboParser 2.1.0"
| #define PACKAGE_BUGREPORT "afm@cs.cmu.edu"
| #define PACKAGE_URL "http://www.ark.cs.cmu.edu/TurboParser/"
| #define STDC_HEADERS 1
| #define HAVE_SYS_TYPES_H 1
| #define HAVE_SYS_STAT_H 1
| #define HAVE_STDLIB_H 1
| #define HAVE_STRING_H 1
| #define HAVE_MEMORY_H 1
| #define HAVE_STRINGS_H 1
| #define HAVE_INTTYPES_H 1
| #define HAVE_STDINT_H 1
| #define HAVE_UNISTD_H 1
| #define HAVE_STDDEF_H 1
| #define HAVE_STRING_H 1
| #define HAVE_SYS_TIME_H 1
| /* end confdefs.h. */
| #include <stdio.h>
| #ifdef HAVE_SYS_TYPES_H
| # include <sys/types.h>
| #endif
| #ifdef HAVE_SYS_STAT_H
| # include <sys/stat.h>
| #endif
| #ifdef STDC_HEADERS
| # include <stdlib.h>
| # include <stddef.h>
| #else
| # ifdef HAVE_STDLIB_H
| # include <stdlib.h>
| # endif
| #endif
| #ifdef HAVE_STRING_H
| # if !defined STDC_HEADERS && defined HAVE_MEMORY_H
| # include <memory.h>
| # endif
| # include <string.h>
| #endif
| #ifdef HAVE_STRINGS_H
| # include <strings.h>
| #endif
| #ifdef HAVE_INTTYPES_H
| # include <inttypes.h>
| #endif
| #ifdef HAVE_STDINT_H
| # include <stdint.h>
| #endif
| #ifdef HAVE_UNISTD_H
| # include <unistd.h>
| #endif
| int
| main ()
| {
| if (sizeof ((_Bool)))
| return 0;
| ;
| return 0;
| }
configure:4624: result: yes
configure:4640: checking for inline
configure:4656: gcc -c -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4656: $? = 0
configure:4664: result: inline
configure:4686: checking for gettimeofday
configure:4686: gcc -o conftest -g -O2 -I../../deps/local/include -I/ad3 conftest.c >&5
configure:4686: $? = 0
configure:4686: result: yes
configure:4836: creating ./config.status
答案 1 :(得分:1)
尝试使用filter
方法:
ABC.objects.filter(B__isnull=False).values('A', 'B')
如您所述,也可以使用exclude
:
ABC.objects.exclude(B__isnull=True).values('A', 'B')
exclude方法与过滤器相同,只是在匹配的条件周围放置not
。它们是完美的对立面,只是代码在某些情况下会使用过滤器更好地读取,并且在其他条件下排除。
文档中有one minor note关于何时调用exclude
和values
这样的调用,但它没有合理的区别:
最后,请注意,ValuesQuerySet是QuerySet的子类,它实现了大多数相同的方法。您可以在其上调用filter(),order_by()等。这意味着这两个调用是相同的:
Blog.objects.values().order_by('id')
Blog.objects.order_by('id').values()
制作Django的人更喜欢首先放置所有影响SQL的方法,然后(可选)使用任何影响输出的方法(例如values()),但这并不重要。这是你真正炫耀个人主义的机会。