django模型创建索引_Django模型

时间:2024-05-18 21:10:00

django模型创建索引

In this tutorial, we’re going to learn about models in django.

在本教程中,我们将学习django中的模型。

Models are connected to our database. We can also say that a model contains the fields and behaviors of the data that we want to store into database. Normally each model is connected to a single table of our database.

模型已连接到我们的数据库。 我们也可以说模型包含要存储到数据库中的数据的字段和行为。 通常,每个模型都连接到数据库的单个表。

To understand models and upcoming topics more easily. Let’s create a new project. Where we’ll make a website like this:

更轻松地了解模型和即将到来的主题。 让我们创建一个新项目。 我们将在哪里建立这样的网站:

django模型创建索引_Django模型

In above picture, we have a web page (homepage) where we’re going to show some jobs that one have done in the past. Normally each of job block will contain a picture and some details. Where the pictures and details will be picked from database, which will make our website dynamic.

在上面的图片中,我们有一个网页(主页),其中将显示一个人过去完成的工作。 通常,每个作业块都会包含一张图片和一些细节。 图片和详细信息将从数据库中提取的地方,这将使我们的网站充满活力。

To start, create a new project  (I’m creating as my_project).

首先,创建一个新项目(我将作为my_project创建)。

First we’ll create an app for all the jobs so if you want to show that jobs in any other project, then you can move that app into other project (as we’ve seen in previous article) .

首先,我们将为所有作业创建一个应用程序,因此,如果您想在其他任何项目中显示该作业,则可以将该应用程序移至其他项目(如上一篇文章中所见)。

Let’s say we have created an app named as jobs by using the command below. Open command prompt and navigate to your project folder, then type:

比方说,我们通过使用下面的命令创建一个名为作为工作应用程序 。 打开命令提示符并导航到您的项目文件夹,然后键入:

python manage.py startapp jobs

python manage.py startapp作业

And don’t forget to add the path of our newly created app in settings.py file of our project folder. (follow the article working with apps to do that).

并且不要忘记在项目文件夹的settings.py文件中添加我们新创建的应用程序的路径。 (按照与应用程序一起工作的文章进行操作)。

Note: Use python3 for python 3 in linux

注意:在Linux中将python3用于python 3

I hope you’ve created project and an app named as jobs.

我希望您已经创建了一个项目和一个名为Jobs的应用程序

Here is how our project folder look like.

这是我们的项目文件夹的样子。

django模型创建索引_Django模型

You can see, after creating app we’ve a auto created file named as models.py. Each app will have separate model file. So we can work with our database from that model file.

您可以看到,创建应用程序后,我们有一个自动创建的文件,名为models.py。 每个应用程序将具有单独的模型文件。 因此,我们可以从该模型文件中使用数据库。

在Django中创建模型 (Create a Model in Django)

To create a Model for storing or retrieving information from database, create a new class with any name you want and inherit the class Model. Now open that model file and edit as below:

要创建用于存储数据库信息或从数据库检索信息的模型,请使用所需的任何名称创建一个新类,并继承该类Model。 现在打开该模型文件并进行如下编辑:

1
2
3
4
5
6
7
8
9
from django.db import models
# Create your models here.
class Job(models.Model):
    #for image
    image = models.ImageField(upload_to='images/')
    #for details
    summary  = models.CharField(max_length=200)
1
2
3
4
5
6
7
8
9
from django . db import models
# Create your models here.
class Job ( models . Model ) :
     #for image
     image = models . ImageField ( upload_to = 'images/' )
     #for details
     summary    = models . CharField ( max_length = 200 )

Here we’re creating a class inside our models.py file. Where we’re using ImageField to store image for a job and CharField to store the summary or details of that particular job. Inside ImageField the attribute upload_to=’images/’  denotes the directory where our images will be stored and the attribute max_length=200 is denoting that we can store maximum 200 inside our CharField named as summary.

在这里,我们在models.py文件中创建一个类。 我们在其中使用ImageField存储作业的图像,并使用CharField存储该特定作业的摘要或详细信息的位置。 在ImageField内部,属性upload_to ='images /'   表示将要存储图像的目录,属性max_length = 200表示我们最多可以在名为摘要的CharField中存储200个

告诉Django项目我们已经添加了新模型 (Tell Django Project That We’ve Added a New Model)

After creating any model we have to change something in our project, so our project will get to know that we have a new model to use.

创建任何模型后,我们必须在项目中进行某些更改,因此我们的项目将知道我们有一个新模型可以使用。

Before doing this you may have noticed some warnings while running the server, as shown below:

在执行此操作之前,您可能会在运行服务器时注意到一些警告,如下所示:

django模型创建索引_Django模型

It is showing that we have 15 unapplied migrations for app(s). In warning, it is also showing that how to apply those migrations.

表明我们有15个未应用的迁移。 在警告中,它还显示了如何应用这些迁移。

So stop your server and type the command below and restart the server.

因此,停止服务器并在下面键入命令,然后重新启动服务器。

python manage.py migrate

python manage.py迁移

And after restart you can see there is now warning now.

重新启动后,您现在可以看到警告。

Migration done.

迁移完成。

And another thing to do before adding our model is to add something in our settings.py file in our project folder.

在添加模型之前,要做的另一件事是在项目文件夹的settings.py文件中添加一些内容。

django模型创建索引_Django模型

We’ve added MEDIA_ROOT and MEDIA_URL.

我们添加了MEDIA_ROOTMEDIA_URL

Here MEDIA_ROOT is showing that if someone wants to save some sort of information (image, video etc.) it should happen inside the folder media.

MEDIA_ROOT在此显示,如果有人要保存某种信息(图像,视频等),则应在文件夹media内进行

And MEDIA_URL showing that if someone is trying to get the access to the image where they are going to get that.

MEDIA_URL显示,如果有人试图获得对图像的访问权,那么他们将在那里获得图像。

But it still doesn’t have the idea that we have added a new model called Job. To get it known, open terminal and type:

但是它仍然不知道我们添加了一个名为Job的新模型。 要知道它,请打开终端并输入:

python manage.py makemigrations

python manage.py makemigrations

django模型创建索引_Django模型

Then type another command given below:

然后键入下面给出的另一个命令:

python manage.py migrate

python manage.py迁移

Now we’re upto date, there is nothing to migrate.

现在我们是最新的,没有什么可迁移的。

That’s all about the model. We’re all set to working with the database. By default sqlite3 database will be used. The advantage of using models is that if we want to change the database later, we can easily change it without changing our models.py file. It will remain the same for all the database.

这就是关于模型的全部。 我们都准备使用数据库。 默认情况下,将使用sqlite3数据库。 使用模型的优点是,如果我们以后想要更改数据库,则可以轻松更改它而无需更改我们的models.py文件。 所有数据库将保持相同。

In next tutorial we’ll learn about how to upload images and summary (text) in database using this module.

在下一个教程中,我们将学习如何使用该模块在数据库中上传图像和摘要(文本)。

翻译自: https://www.thecrazyprogrammer.com/2019/01/django-models.html

django模型创建索引