-1
我最近從PHP遷移到django。我正在創建一個在Django上工作的項目..我習慣於在PHP中編寫自定義sql,因此我想使用raw()在django中過濾來自我的數據庫的結果。在Django中使用原始SQL
但是我不能夠完全理解Django是如何運作..
請在下面找到我的代碼。
目前我收到下面提到的輸出
[('11677795635',), ('12345',)]
我想用一些在下文提到的格式環和印刷效果。
可以請你幫我如何for循環在Django ..
在PHP中,
同樣有可能通過
$query=mysql_query("SELECT abcdashboard_customerinfo.customerbuyingid FROM abcdashboard_customerinfo WHERE abcdashboard_customerinfo.customerbuyingid in (select DISTINCT abcdashboard_orders.customerid from abcdashboard_orders)");
$queryrun=mysql_num_rows($query);
for ($f=0; $f <$queryrun; $f++)
{
${'customer'.$f}=mysql_result($query,$f, 'customerbuyingid');
echo ${'customer'.$f};
}
models.py
class customerinfo(models.Model):
customerbuyingid = models.CharField(max_length=300, default='invalid customer id in database')
customername = models.CharField(max_length=300, default='invalid customer name in database')
customerphonenumber = models.CharField(max_length=12, default='0000000000')
customermailid= models.CharField(max_length=80, default='[email protected]')
class orders(models.Model):
orderid = models.CharField(max_length=200)
orderstatus = models.CharField(max_length=10)
externalpurchaseid = models.CharField(max_length=100)
externalstatus = models.CharField(max_length=100)
customerid = models.CharField(max_length=100)
ops = models.CharField(max_length=100)
orderdate = models.DateField()
updatedon = models.DateTimeField(default=timezone.now)
views.py
def index(request):
all_customer = customerinfo.objects.all()
cursor = connection.cursor()
cursor.execute('''SELECT abcdashboard_customerinfo.customerbuyingid FROM abcdashboard_customerinfo WHERE abcdashboard_customerinfo.customerbuyingid in (select DISTINCT abcdashboard_orders.customerid from abcdashboard_orders) ''')
row = cursor.fetchall()
print (row)
context = {"row": row}
return render(request, "abcdashboard/index.html", context)
的Index.html
<html>
<head>
<title>
</title>
</head>
<body>
<ul>
<h2 align="center"> SQL Queries display </align> </h2>
{% block content %}
{{ row }}
{% endblock %}
</ul>
</body>
</html>
不要這樣做。如果你想編寫Django,你應該編寫Django –