Django中ORM找出內(nèi)容不為空的數(shù)據(jù)實例
在django操作數(shù)據(jù)庫的時候如何找出內(nèi)容不為空的數(shù)據(jù)呢?
from django.db.models import Q
class Index(VIew):
def get(self, request):
userObj = models.Asset.objects.filter(~Q(asset_id = '')
return HttpResponse('yes')
上面代碼中的models.Asset.objects.filter(~Q(nick = '')則是使用Q函數(shù)去找出nick不為空的數(shù)據(jù),主要使用~Q
補充知識:Django報錯 HINT: Add or change a related_name argument to the definition for 'GodownentryReturn.suppl
Unhandled exception in thread started by <function wrapper at 0x05569030>
Traceback (most recent call last):
File “G:\workspace\pycharm\carwin\carwin_env\lib\site-packages\django\utils\autoreload.py”, line 226, in wrapper
fn(*args, **kwargs)
File “G:\workspace\pycharm\carwin\carwin_env\lib\site-packages\django\core\management\commands\runserver.py”, line 116, in inner_run
self.check(display_num_errors=True)
File “G:\workspace\pycharm\carwin\carwin_env\lib\site-packages\django\core\management\base.py”, line 472, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:ERRORS:
article.GodownentryReturn.supplier: (fields.E304) Reverse accessor for ‘GodownentryReturn.supplier' clashes with reverse accessor for ‘GodownentryReturn.supplier'.
HINT: Add or change a related_name argument to the definition for ‘GodownentryReturn.supplier' or ‘GodownentryReturn.supplier'.
part.GodownentryReturn.supplier: (fields.E304) Reverse accessor for ‘GodownentryReturn.supplier' clashes with reverse accessor for ‘GodownentryReturn.supplier'.
HINT: Add or change a related_name argument to the definition for ‘GodownentryReturn.supplier' or ‘GodownentryReturn.supplier'.
原因:
因為在一個表中多次引用某個外鍵表,而且沒有指定唯一的releated_name,而導致的。
解決方案:
將同一個表中外鍵,增加不同的releated_name。
以上這篇Django中ORM找出內(nèi)容不為空的數(shù)據(jù)實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
flask-socketio實現(xiàn)WebSocket的方法
這篇文章主要介紹了flask-socketio實現(xiàn)WebSocket的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
Python+wxPython實現(xiàn)文件名批量處理
在日常的文件管理中,我們經(jīng)常需要對文件進行批量處理以符合特定的命名規(guī)則或需求,本文主要介紹了如何使用wxPython進行文件夾中文件名的批量處理,需要的可以參考下2024-04-04
詳解python中@classmethod和@staticmethod方法
在python類當中,經(jīng)常會遇到@classmethod和@staticmethod這兩個裝飾器,那么到底它們的區(qū)別和作用是啥子呢?本文結(jié)合場景分析給大家詳細講解,感興趣的朋友跟隨小編一起看看吧2022-10-10
python3:excel操作之讀取數(shù)據(jù)并返回字典 + 寫入的案例
這篇文章主要介紹了python3:excel操作之讀取數(shù)據(jù)并返回字典 + 寫入的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
跟老齊學Python之傳說中的函數(shù)編寫條規(guī)
在使用函數(shù)的時候,首先要把它放在對象的層面考量,它不是什么特殊的東西,盡管我們使用了不少篇幅講述它,但它終歸還是一個對象。2014-10-10
基于python中pygame模塊的Linux下安裝過程(詳解)
下面小編就為大家?guī)硪黄趐ython中pygame模塊的Linux下安裝過程(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

