jango中使用動態語句查詢

2021-09-26 14:40:07 字數 783 閱讀 2591

1. 特殊業務條件下,使用者的資料展示必須根據【資料庫的中某字段的條件配置】進行【過濾後展示】

而這個展示條件,是在頁面上進行配置的,即在頁面上動態修改

而這個動態修改會要求django查詢資料時,要根據頁面上的配置進行查詢,故需要下面的方式,進行複雜過濾查詢

from django.db.models import q

from django.shortcuts import render

# create your views here.

from django.test import testcase

from dynamic_db_router import in_database

from webservice.models import t_database_conf

class testindatabasecontextmanager(testcase):

external_db =

with in_database(external_db):

# con=q()

q1=q()

q1.connector = "and"

q2=q()

q2.connector = "and"

q1.add(q2, "or")

#con.add(q2, "or")

obj = t_database_conf.objects.filter(q1)

這裡不使用sql語句直接查詢,依舊使用filter,極端條件下使用sql語句查詢!

iBatis中使用動態查詢

prepend and open close isnotnullproperty ki.userid prepend and removefirstprepend false user id ki.userid isnotnullproperty kfi.initiator prepend and ...

在django中使用sql語句查詢

django中提供了乙個raw 方法來使用sql語句進行查詢 class person models.model first name models.charfield max length 50 last name models.charfield max length 50 birth date...

django中使用原生SQL語句查詢

django使用原生sql語句查詢兩種方式 raw查詢 例項 查詢租房專案的經理人資訊 agent.objects.raw sql語句 返回乙個物件,但是還沒有繞過orm框架使用django中的connections來寫sql語句 from django.db import connections ...