从Django模型创建数据库视图 [英] Create database view from django model
问题描述
我将sql视图作为一个虚拟表来学习,以简化SQL操作,例如
MySQL [distributor]>创建视图CustomerEMailList AS
->选择cust_id,cust_name,cust_email
->来自客户
-> cust_email不为空;
查询正常,受影响的0行(0.026秒)
MySQL [distributor]>从客户电子邮件列表中选择*;
+ ------------ + --------------- + ---------------- ------- +
| cust_id | cust_name | cust_email |
+ ------------ + --------------- + ---------------- ------- +
| 1000000001 |乡村玩具| sales@villagetoys.com |
| 1000000003 | Fun4All | jjones@fun4all.com |
| 1000000004 | Fun4All | dstephens@fun4all.com |
| 1000000005 |玩具商店| kim@thetoystore.com |
| 1000000006 |玩具乐园| sam@toyland.com |
+ ------------ + --------------- + ---------------- ------- +
集合中的5行(0.014秒)
何时我随后查看了Django文档,没有创建虚拟模型表的功能,可以简化数据操作。
我应该忘记虚拟表视图在使用Django ORM时?
Django就我所知,目前还没有内置的视图支持。 / p>
但是您可以使用> django-database-view
软件包。
安装软件包后(例如使用pip):
pip安装django-database-view
此外,必须在 dbview
应用中注册 settings.py
文件:
#settings.py
INSTALLED_APPS =(
#...
'dbview',
#...
)
现在您可以构建视图了,这看起来与 的构建有点相似一个模型,除了需要实现 view(..)
函数,该函数指定视图后的查询。类似于以下内容:
从django.db导入模型
从dbview导入DbView
class CustomerEMailList(DbView):
cust = models.OneToOneField(Customer,primary_key = True)
cust_name = models.CharField()
cust_email = models.CharField()
@classmethod
def view(klass):
qs =(Customers.objects.filter(cust_email__isnull = False)
.values('cust_id','cust_name','cust_email') )
return str(qs.query)
现在我们可以进行迁移了:
./ manage.py makemigrations
现在在迁移中,我们需要进行更改:对 migrations.CreateModel
的调用与构造的视图相关的strong>应该更改为 dbview
的 CreateView
code>模块。看起来像这样:
从django.db导入迁移
从dbview导入CreateView
类Migration(migrations.Migration):
依赖关系= []
操作= [
CreateView(
name ='CustomerEMailList',
fields = [
#...
],
),
]
I learned sql "view" as a virtual table to facilitate the SQL operations, like
MySQL [distributor]> CREATE VIEW CustomerEMailList AS
-> SELECT cust_id, cust_name, cust_email
-> FROM Customers
-> WHERE cust_email IS NOT NULL;
Query OK, 0 rows affected (0.026 sec)
MySQL [distributor]> select * from customeremaillist;
+------------+---------------+-----------------------+
| cust_id | cust_name | cust_email |
+------------+---------------+-----------------------+
| 1000000001 | Village Toys | sales@villagetoys.com |
| 1000000003 | Fun4All | jjones@fun4all.com |
| 1000000004 | Fun4All | dstephens@fun4all.com |
| 1000000005 | The Toy Store | kim@thetoystore.com |
| 1000000006 | toy land | sam@toyland.com |
+------------+---------------+-----------------------+
5 rows in set (0.014 sec)
When I checked the Django documentation subsequently, there are no such functionality to create a virtual "model table" which could simplify the data manipulation.
Should I forget the virtual table "view" when using Django ORM?
Django has - as far as I know at the moment - no builtin support for views.
But you can construct such views, by using the django-database-view
package.
After installing the package (for example with pip):
pip install django-database-view
Furthermore the dbview
app has to be registered in the settings.py
file:
# settings.py
INSTALLED_APPS = (
# ...
'dbview',
# ...
)
Now you can construct a view, this looks a bit similar to the construction of a model, except that you need to implement a view(..)
function that specifies the query behind the view. Something similar to:
from django.db import models
from dbview import DbView
class CustomerEMailList(DbView):
cust = models.OneToOneField(Customer, primary_key=True)
cust_name = models.CharField()
cust_email = models.CharField()
@classmethod
def view(klass):
qs = (Customers.objects.filter(cust_email__isnull=False)
.values('cust_id', 'cust_name', 'cust_email'))
return str(qs.query)
Now we can make a migrations:
./manage.py makemigrations
Now in the migration, we need to make a change: the calls to migrations.CreateModel
that are related to the constructed view(s), should be changed to the CreateView
of the dbview
module. Something that looks like:
from django.db import migrations
from dbview import CreateView
class Migration(migrations.Migration):
dependencies = []
operations = [
CreateView(
name='CustomerEMailList',
fields=[
# ...
],
),
]
这篇关于从Django模型创建数据库视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!