博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django模型_如何简单理解Django模型
阅读量:2523 次
发布时间:2019-05-11

本文共 4344 字,大约阅读时间需要 14 分钟。

django模型

by Tim

提姆(Tim)

如何简单理解Django模型 (How to understand Django models the simple way)

Have you ever tried to learn models by going through Django Docs? Did you leave with answers, or with even more questions?

您是否曾经尝试通过Django Docs学习模型? 您离开时留下的答案还是更多的问题?

Personally, I started doubting whether programming was really for me.

我个人开始怀疑编程是否真的适合我。

I wrote this post to help you understand Django models so you can perform basic operations with them. Models are an excellent way to work with data.

我写这篇文章是为了帮助您了解Django模型,以便您可以对它们执行基本操作。 模型是处理数据的绝佳方法。

Say we want to keep track of our amazing cats. We could create a Cat model — but what is a model anyway?

假设我们想跟踪我们的神奇猫。 我们可以创建Cat模型-但是什么是模型?

Turns out, a model is kind of three things in one:

事实证明,模型是三者合一的:

Now, let’s walk through each block.

现在,让我们遍历每个块。

表与数据库中的猫 (Table with cats in database)

We’ve created a class (= a model) named Cat.

我们创建了一个名为Cat的类(=模型)。

When we add columns, we must tell Django which kind of data will be in each of them. It can be string, digits, or boolean, among others.

添加列时,我们必须告诉Django每种类型的数据。 它可以是字符串,数字或布尔值等。

In this case, the cat’s name should be in text — this is a CharField in Django. Don’t forget to set the maximum length for this field, because the database needs to know it. The cat’s weight in grams is an integer — so we use an IntegerField. Just a note: the id column is generated automatically.

在这种情况下,猫的名称应为文本-这是Django中的CharField 。 不要忘记为此字段设置最大长度,因为数据库需要知道它。 猫的重量(以克为单位)是整数,因此我们使用IntegerField 。 请注意: id列是自动生成的。

Finally, null makes it possible to leave a column blank. For example, we might not know the weight. Note that any field can be marked as null.

最后,使用null可以将列留空。 例如,我们可能不知道重量。 注意,任何字段都可以标记为null

For the finishing touch, we’ll propagate changes (like creating a model or adding a column) into our database schema. For that we use python manage.py makemigrations and then python manage.py migrate. It’s important to do this every time you change something in the models.

为了画龙点睛,我们将更改(例如创建模型或添加列)传播到我们的数据库模式中。 为此,我们使用python manage.py makemigrations然后python manage.py migrate 。 每次更改模型中的内容时,务必要执行此操作。

Now we have a table but we don’t have anything inside it. Let’s fix that.

现在我们有了一张桌子,但是里面没有任何东西。 让我们修复它。

所有猫的操作 (Operations with all cats)

创建一个条目 (Creating an entry)

The create() function helps us to create some rows. We just need to pass all the purry details into it.

create()函数可帮助我们创建一些行。 我们只需要将所有purry细节传递给它即可。

寻找一个特定的 (Finding a particular one)

If you want to get the cat’s FBI file — meow-xcuse me, I mean the cat’s info — just use the get() function with one of the cat parameters. In the example, I use pk which means “primary key.” Most often, that would be the same as using id.

如果要获取猫的FBI文件(喵喵叫我,我的意思是猫的信息),只需将get()函数与cat参数之一一起使用。 在示例中,我使用pk表示“主键”。 大多数情况下,这与使用id相同。

get() will find all rows matching the parameters and will only return the first one.

get()将查找与参数匹配的所有行,并且仅返回第一个。

查找所有记录 (Finding all the records)

On top of that, you can access all cats from the database by using the all() function.

最重要的是,您可以使用all()函数访问数据库中的all() cat。

过滤掉 (Filtering out)

Or do you need cats lighter than 3000g?

还是您需要重量超过3000克的猫?

A function named filter is ready to help you with that.

准备使用名为filter的功能来帮助您。

We pass field__lookuptype = 'value' into it to filter out the cats.

我们将field__lookuptype = 'value'传递给它以过滤掉猫。

In the example, lt means “less than.” So weight_g__lt=3000 means “weight is less than 3000g.”

在此示例中, lt表示“小于”。 因此weight_g__lt=3000表示“重量小于3000g”。

一只猫的操作 (Operations with one cat)

更新中 (Updating)

Last time we weighed Luna, she was 3200g. But now her weight is 3100g. It’s very easy to change that.

上次我们称重Luna的重量是3200g。 但是现在她的体重是3100克。 改变它很容易。

We just get Luna from the database by her name, and then change her weight to 3100. It’s that simple. Just one thing — we have to call .save() when we’re done changing.

我们只是从数据库中获得Luna的名字,然后将其体重更改为3100。就这么简单。 只是一件事-完成更改后,我们必须调用.save()

永久删除 (Deleting, like, forever)

We can delete one of our cats. We get the cat and call the .delete() function.

我们可以删除一只猫。 我们得到这只猫,并调用.delete()函数。

Very sad. But that’s life.

很伤心 但这就是生活。

Did you enjoy this article? Please give me some claps so more people see it. Thanks!

你喜欢这篇文章吗? 请给我一些鼓掌,以便更多的人看到它。 谢谢!

The article was originally published on .

这篇文章最初发表在 。

to get my new articles in your inbox and learn Django together.

即可在收件箱中获取我的新文章,并一起学习Django。

翻译自:

django模型

转载地址:http://pbzzd.baihongyu.com/

你可能感兴趣的文章
mac下多线程实现处理
查看>>
C++ ifstream ofstream
查看>>
跟初学者学习IbatisNet第四篇
查看>>
seL4环境配置
查看>>
Git报错:insufficient permission for adding an object to repository database .git/objects
查看>>
ajax跨域,携带cookie
查看>>
BZOJ 1600: [Usaco2008 Oct]建造栅栏( dp )
查看>>
洛谷 CF937A Olympiad
查看>>
Codeforces Round #445 C. Petya and Catacombs【思维/题意】
查看>>
用MATLAB同时作多幅图
查看>>
python中map的排序以及取出map中取最大最小值
查看>>
ROR 第一章 从零到部署--第一个程序
查看>>
<form>标签
查看>>
vue去掉地址栏# 方法
查看>>
Lambda03 方法引用、类型判断、变量引用
查看>>
was集群下基于接口分布式架构和开发经验谈
查看>>
MySQL学习——MySQL数据库概述与基础
查看>>
ES索引模板
查看>>
HDU2112 HDU Today 最短路+字符串哈希
查看>>
JPanel重绘
查看>>