English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Django Model (Model)

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

创建模型

下面是创建一个 Dreamreal 模型实例 -

# Filename: example.py
# Copyright: 2020 By w3codebox
# Author by: www.oldtoolbag.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 w3codebox
# Author by: www.oldtoolbag.com
# Date: 2020-08-08
$python manage.py syncdb

操作数据(CRUD)

让我们创建一个“crudops”视图,看看如何在模型上执行CRUD操作。现在 myapp/views.py 然后,看起来像 -

myapp/views.py

# Filename: example.py
# Copyright: 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date: 2020-08-08
from myapp.models import Dreamreal
 from django.http import HttpResponse
 def crudops(request):
    #创建条目
    dreamreal = Dreamreal(
       website = "www.polo.com", mail = "[email protected]", 
       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 = "[email protected]", 
       name = "sorex", phonenumber = "002376970"
    )
    dreamreal.save()
    res += 'Updating entry<br>'
    dreamreal = Dreamreal.objects.get(name = 'sorex')
    dreamreal.name = 'thierry'
    dreamreal.save()
    return HttpResponse(res)

Other data operations

Let's explore other operations that can be done on the model. It should be noted that CRUD operations are performed on model instances, and now we will directly represent the working of the model class.

Let's create a "datamanipulation" view in myapp/views.py

# Filename: example.py
# Copyright: 2020 By w3codebox
# Author by: www.oldtoolbag.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)

model linking

Django ORM provides3This way to link models -

The first example we will see here is a one-to-many relationship. As seen in the above example, a company can have multiple online websites. This relationship is defined by using django.db.models.ForeignKey -

myapp/models.py

# Filename: example.py
# Copyright: 2020 By w3codebox
# Author by: www.oldtoolbag.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"

Update myapp/models.py, as you can see, we added an online mode and linked it to the Dreamreal model.

Let's see how to perform all the work through the manage.py shell -

First, let's test the Django shell to create some companies (Dreamreal items) -

# Filename: example.py
# Copyright: 2020 By w3codebox
# Author by: www.oldtoolbag.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()

Now there are some proxy network domains -

# Filename: example.py
# Copyright: 2020 By w3codebox
# Author by: www.oldtoolbag.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()

It is very simple to access the properties of the hosting company (Dreamreal item) from the online domain -

 # Filename: example.py
# Copyright: 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date: 2020-08-08
>>> on1.company.name

If you want to know all the online domains hosted by the company Dreamreal, we will use the code -

# Filename: example.py
# Copyright: 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date: 2020-08-08
>>> dr1.online_set.all()

To get a QuerySet, note that all the operation methods we have seen before (filter, all, exclude, order_by...)

You can also access link model properties for filtering operations, for example, to get all online domains in Dreamreal with names containing 'company'-

# Filename: example.py
# Copyright: 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date: 2020-08-08
>>> Online.objects.filter(company__name__contains='company')

Note - This query only supports SQL databases. It will not work on non-relational databases where connections do not exist and there are two "_".

However, this is not the only way to link models, there is also OneToOneField, which ensures that the relationship between two objects is a unique linking relationship. If OneToOneField is used in the above example, it means that there is only one online entry corresponding to each Dreamreal entry.

The relationships between ManyToManyField tables (NN) are all based on SQL databases.