模型是表示我们的数据库表或集合类,并且其中所述类的每个属性是表或集合的字段。模型是在 app/models.py 中定义(在我们的实例中是:myapp/models.py)
下面是创建一个 Dreamreal 模型实例 −
# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.cainiaojc.com
# Date : 2020-08-08
from django.db import models
class Dreamreal(models.Model):
website = models.CharField(max_length = 50)
mail = models.CharField(max_length = 50)
name = models.CharField(max_length = 50)
phonenumber = models.IntegerField()
class Meta:
db_table = "dreamreal"
每一个模型继承自django.db.models.Model。
我们类有4个属性(3 CharField和1个整数),这将是表中的字段。
Meta类与db_table属性可以让我们定义的实际表或集合名称。Django会自动命名表或集合:myapp_modelName. 这个类将强制表的名称。
在 django.db.models 更多的字段的类型,你可以了解更多关于他们的URL:
https://docs.djangoproject.com/en/1.5/ref/models/fields/#field-types
在创建模型后需要 Django 产生实际的数据库 −
# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.cainiaojc.com
# Date : 2020-08-08
$python manage.py syncdb
让我们创建一个“crudops”的视图,看看如何能够在模型上做的CRUD操作。 现在 myapp/views.py 然后将看起来像 −
myapp/views.py
# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.cainiaojc.com
# Date : 2020-08-08
from myapp.models import Dreamreal
from django.http import HttpResponse
def crudops(request):
#Creating an entry
dreamreal = Dreamreal(
website = "www.polo.com", mail = "sorex@polo.com",
name = "sorex", phonenumber = "002376970"
)
dreamreal.save()
#Read ALL entries
objects = Dreamreal.objects.all()
res ='Printing all Dreamreal entries in the DB : <br>'
for elt in objects:
res += elt.name+"<br>"
#Read a specific entry:
sorex = Dreamreal.objects.get(name = "sorex")
res += 'Printing One entry <br>'
res += sorex.name
#Delete an entry
res += '<br> Deleting an entry <br>'
sorex.delete()
#Update
dreamreal = Dreamreal(
website = "www.polo.com", mail = "sorex@polo.com",
name = "sorex", phonenumber = "002376970"
)
dreamreal.save()
res += 'Updating entry<br>'
dreamreal = Dreamreal.objects.get(name = 'sorex')
dreamreal.name = 'thierry'
dreamreal.save()
return HttpResponse(res)
让我们来探讨可以对模型做的其他操作。 需要注意的是 CRUD 操作都做对模型的实例,现在我们将直接表示模型类的工作。
让我们创建一个“datamanipulation”视图在 myapp/views.py
# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.cainiaojc.com
# Date : 2020-08-08
from myapp.models import Dreamreal
from django.http import HttpResponse
def datamanipulation(request):
res = ''
#Filtering data:
qs = Dreamreal.objects.filter(name = "paul")
res += "Found : %s results<br>"%len(qs)
#Ordering results
qs = Dreamreal.objects.order_by("name")
for elt in qs:
res += elt.name + '<br>'
return HttpResponse(res)
Django ORM提供3种方式来链接模型 −
我们将在这里看到的第一示例是一个一对多的关系。正如在上面的实例中看到的,一个公司可以有多个在线网站。定义这种关系是通过使用 django.db.models.ForeignKey 完成 -
myapp/models.py
# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.cainiaojc.com
# Date : 2020-08-08
from django.db import models
class Dreamreal(models.Model):
website = models.CharField(max_length = 50)
mail = models.CharField(max_length = 50)
name = models.CharField(max_length = 50)
phonenumber = models.IntegerField()
online = models.ForeignKey('Online', default = 1)
class Meta:
db_table = "dreamreal"
class Online(models.Model):
domain = models.CharField(max_length = 30)
class Meta:
db_table = "online"
可以更新myapp/models.py,正如你看到的,我们添加了一个在线模式,并链接到 Dreamreal模型。
让我们来看看如何通过 manage.py shell 执行所有工作 −
首先让我们来测试 Django shell创建一些公司(Dreamreal项)−
# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.cainiaojc.com
# Date : 2020-08-08
$python manage.py shell
>>> from myapp.models import Dreamreal, Online
>>> dr1 = Dreamreal()
>>> dr1.website = 'company1.com'
>>> dr1.name = 'company1'
>>> dr1.mail = 'contact@company1'
>>> dr1.phonenumber = '12345'
>>> dr1.save()
>>> dr2 = Dreamreal()
>>> dr1.website = 'company2.com'
>>> dr2.website = 'company2.com'
>>> dr2.name = 'company2'
>>> dr2.mail = 'contact@company2'
>>> dr2.phonenumber = '56789'
>>> dr2.save()
现在有一些代管网域 −
# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.cainiaojc.com
# Date : 2020-08-08
>>> on1 = Online()
>>> on1.company = dr1
>>> on1.domain = "site1.com"
>>> on2 = Online()
>>> on2.company = dr1
>>> on2.domain = "site2.com"
>>> on3 = Online()
>>> on3.domain = "site3.com"
>>> dr2 = Dreamreal.objects.all()[2]
>>> on3.company = dr2
>>> on1.save()
>>> on2.save()
>>> on3.save()
从在线域访问托管公司(Dreamreal项)的属性是很简单的 −
# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.cainiaojc.com
# Date : 2020-08-08
>>> on1.company.name
如果想知道公司Dreamreal主办的所有网上域名,我们将使用代码 −
# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.cainiaojc.com
# Date : 2020-08-08
>>> dr1.online_set.all()
为了得到一个QuerySet,请注意,所有的操作方法,我们以前见过(filter, all, exclude, order_by....)
也可以访问进行过滤操作链接模型属性,比方说,想获得的所有在线域所在Dreamreal名称包含“company”-
# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.cainiaojc.com
# Date : 2020-08-08
>>> Online.objects.filter(company__name__contains = 'company'
注 - 那种查询只支持SQL数据库。 它不会对非关系数据库工作,其中连接不存在,并有两个“_”。
但是,这不是链接模型的唯一方法,也有OneToOneField,这保证了两个对象之间的关系是唯一的链接关系。如果使用了OneToOneField在上面的实例中,这将意味着只有一个在线条目对应于每个Dreamreal条目。
最后一个,ManyToManyField 表之间(NN)的关系这些都是基于SQL的数据库。