简体   繁体   English

在 Django model 的情况下,

[英]In case of Django model,

In the django code in order to make a model,在 django 代码中以制作 model,

for example,例如,

class Student(models.Model):
    name = models.CharField(max_length = 200) 

Why models.CharField(max_length = 200).为什么是models.CharField(max_length = 200)。 Why can't we write only name=CharField(max_length = 200)为什么我们不能只写name=CharField(max_length = 200)

You can .你可以 You should only import that field in the scope, for example with:您应该只在 scope 中导入该字段,例如:

from django.db import models
from django.db.models import CharField  # 🖘 import CharField

class Student(models.Model):
    name = CharField(max_length=200)  # 🖘 use CharField

The field classes like CharField , IntegerField , … are all members of the django.db.models module, so you can import these in the head of the file. CharFieldIntegerField等字段类都是django.db.models模块的成员,因此您可以在文件头部导入它们。

You can also import Model and thus work with:您还可以导入Model并因此使用:

from django.db import models
from django.db.models import CharField, Model  # 🖘 import Model

class Student(Model):  # 🖘 use Model
    name = CharField(max_length=200)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM