本文共 4344 字,大约阅读时间需要 14 分钟。
django模型
by Tim
提姆(Tim)
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.
现在,让我们遍历每个块。
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.
现在我们有了一张桌子,但是里面没有任何东西。 让我们修复它。
The create()
function helps us to create some rows. We just need to pass all the purry details into it.
create()
函数可帮助我们创建一些行。 我们只需要将所有purry细节传递给它即可。
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()
将查找与参数匹配的所有行,并且仅返回第一个。
On top of that, you can access all cats from the database by using the all()
function.
最重要的是,您可以使用all()
函数访问数据库中的all()
cat。
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”。
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()
。
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/