Python Django 使用数据库中的视图

1.在AppDatabaseViews项目中,创建 mysqlviews.py模块。
模块中的内容如下:
from AppDatabaseViews.models import * #
#AppDatabaseViews 是项目工程

class viewsbookandpublish(models.Model):
id = models.BigIntegerField(primary_key=True)
title = models.CharField(max_length=32)
price = models.DecimalField(max_digits=8,decimal_places=2)
publish_date = models.DateField(auto_now=True)
publish_id = models.BigIntegerField()
kucunshuliang = models.BigIntegerField()
maichushuliang = models.BigIntegerField()
ids = models.BigIntegerField()
name = models.CharField(max_length=32)
addr = models.CharField(max_length=64)
email = models.CharField(max_length=254)

class Meta: # 增加本语句
db_table = 'viewsbookandpublish' #为数据库中视图的名称



2.在views.py 中
  
from django.shortcuts import render,HttpResponse

from AppDatabaseViews import mysqlviews # 在AppDatabaseView 中 mysqlviews.py模块

def viewsbookandpublish(request):
obj = mysqlviews.viewsbookandpublish.objects.filter().all()
print(obj.count())
#
# data = serializers.serialize("json", obj)
# return HttpResponse(data)
return render(request,'show.html',locals())



  
原文地址:https://www.cnblogs.com/ttym88m/p/14455972.html