开发手册 欢迎您!
软件开发者资料库

Django - Models

Django Models - 从基础知识,概述,环境,创建项目,应用程序生命周期,管理界面,创建视图,URL映射,模板系统,模型,页面重定向,发送电子邮件,通用视图,表单处理,文件开始学习Django上传,Apache安装,Cookies处理,会话,缓存,评论,RSS,AJAX。

模型是一个表示数据库中表或集合的类,其中类的每个属性都是表或集合的字段.模型在app/models.py中定义(在我们的示例中:myapp/models.py)

创建模型

以下是Dreamreal模型创建为示例 :

from django.db import modelsclass 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个Integer),这些属性就是表字段.

具有db_table属性的元类允许我们定义实际的表或集合名称. Django自动命名表或集合:myapp_modelName.这个类可以让你强制你想要的表名.

django.db.models中有更多字段的类型,你可以在 https://docs.djangoproject.com/en/1.5/ref/models/fields/

创建模型后,您需要Django生成实际数据库 :

$ python manage.py syncdb

操纵数据(CRUD)

让我们创建一个"crudops" "查看我们如何在模型上进行CRUD操作.我们的myapp/views.py看起来像 :

myapp/views.py

from myapp.models import Dreamrealfrom django.http import HttpResponsedef 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 : 
'      for elt in objects:      res += elt.name+"
"      #Read a specific entry:   sorex = Dreamreal.objects.get(name = "sorex")   res += 'Printing One entry 
'   res += sorex.name      #Delete an entry   res += '
 Deleting an entry 
'   sorex.delete()      #Update   dreamreal = Dreamreal(      website = "www.polo.com", mail = "sorex@polo.com",       name = "sorex", phonenumber = "002376970"   )      dreamreal.save()   res += 'Updating entry
'      dreamreal = Dreamreal.objects.get(name = 'sorex')   dreamreal.name = 'thierry'   dreamreal.save()      return HttpResponse(res)

其他数据操作

让我们探索其他操作我们可以在模型上做.请注意,CRUD操作是在我们模型的实例上完成的,现在我们将直接使用代表我们模型的类.

让我们在 myapp/中创建一个'datamanipulation'视图views.py

from myapp.models import Dreamrealfrom django.http import HttpResponsedef datamanipulation(request):   res = ''      #Filtering data:   qs = Dreamreal.objects.filter(name = "paul")   res += "Found : %s results
"%len(qs)      #Ordering results   qs = Dreamreal.objects.order_by("name")      for elt in qs:      res += elt.name + '
'      return HttpResponse(res)

链接模型

Django ORM提供了三种链接模型和减号的方法;

我们将在这里看到的第一个案例是一对多关系.正如您在上面的示例中所看到的,Dreamreal公司可以拥有多个在线网站.定义该关系是通过使用django.db.models.ForeignKey :

myapp/models.py

from django.db import modelsclass 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 : 检查所有这些是如何工作的;

首先让我们创建一些公司(Dreamreal条目)进行测试我们的Django shell :

$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()

现在有些托管域 :

>>> 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条目)的属性很简单 :

>>> on1.company.name

如果我们想知道Dreamreal中公司托管的所有在线域名,我们将使用代码 :

>>> dr1.online_set.all()

要获取QuerySet,请注意我们之前看到的所有操作方法(filter,all,exclude,order_by ... .)

您还可以访问链接模型属性以进行过滤操作,假设您想要获取Dreamreal名称中包含"公司"和减号的所有在线域名;

>>> Online.objects.filter(company__name__contains = 'company'

注意 :  SQL DB只支持这种查询.它不适用于不存在连接的非关系数据库,并且有两个'_'.

但这不是链接模型的唯一方法,你也有OneToOneField,一个链接,保证两个对象之间的关系是唯一的.如果我们在上面的例子中使用了OneToOneField,这意味着每个Dreamreal条目只能有一个在线条目,而在另一个方式.

最后一个,表之间的(nn)关系的ManyToManyField.注意,那些与基于SQL的DB有关.