Django 模型数据类型和字段列表
模型中最重要的部分和唯一需要的部分是它定义的数据库字段列表。字段由类属性指定。注意不要选择与模型 API 冲突的字段名称,例如 clean、save 或 delete。
例子:
from django.db import models
class Musician(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
instrument = models.CharField(max_length=200)
class Album(models.Model):
artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
release_date = models.DateField()
num_stars = models.IntegerField()
设置用于存储任何类型数据的字段就像在 C/C++ 中确定用于存储特定整数、字符等的数据类型一样。Django 中的字段是用于存储特定类型数据的数据类型。例如,要存储整数,将使用 IntegerField。这些字段具有针对特定数据类型的内置验证,即您不能将“abc”存储在 IntegerField 中。同样,对于其他领域。这篇文章围绕一个可以在 Django 模型中使用的主要领域展开。
以下是在开始使用 Django 字段之前应该了解的一些关键属性。
字段类型
模型中的每个字段都应该是相应 Field 类的一个实例。 Django 使用字段类类型来确定一些事情:
- 列类型,它告诉数据库要存储什么样的数据(例如 INTEGER、VARCHAR、TEXT)。
- 呈现表单字段时使用的默认 HTML 小部件(例如 ,
- 在 Django 的管理员和自动生成的表单中使用的最低验证要求。
Django 附带了数十种内置字段类型,它们也可用于保存从数字到整个 HTML 文件的任何类型的数据。这是 Django 中使用的所有字段类型的列表。
基本模型数据类型和字段列表
Field Name | Description |
---|---|
AutoField | It is an IntegerField that automatically increments. |
BigAutoField | It is a 64-bit integer, much like an AutoField except that it is guaranteed to fit numbers from 1 to 9223372036854775807. |
BigIntegerField | It is a 64-bit integer, much like an IntegerField except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807. |
BinaryField | A field to store raw binary data. |
BooleanField | A true/false field. The default form widget for this field is a CheckboxInput. |
CharField | A field to store text-based values. |
DateField | A date, represented in Python by a datetime.date instance |
DateTimeField | It is used for date and time, represented in Python by a datetime.datetime instance. |
DecimalField | It is a fixed-precision decimal number, represented in Python by a Decimal instance. |
DurationField | A field for storing periods of time. |
EmailField | It is a CharField that checks that the value is a valid email address. |
FileField | It is a file-upload field. |
FloatField | It is a floating-point number represented in Python by a float instance. |
ImageField | It inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image. |
IntegerField | It is an integer field. Values from -2147483648 to 2147483647 are safe in all databases supported by Django. |
GenericIPAddressField | An IPv4 or IPv6 address, in string format (e.g. 192.0.2.30 or 2a02:42fe::4). |
NullBooleanField | Like a BooleanField, but allows NULL as one of the options. |
PositiveIntegerField | Like an IntegerField, but must be either positive or zero (0). |
PositiveSmallIntegerField | Like a PositiveIntegerField, but only allows values under a certain (database-dependent) point. |
SlugField | Slug is a newspaper term. A slug is a short label for something, containing only letters, numbers, underscores or hyphens. They’re generally used in URLs. |
SmallIntegerField | It is like an IntegerField, but only allows values under a certain (database-dependent) point. |
TextField | A large text field. The default form widget for this field is a Textarea. |
TimeField | A time, represented in Python by a datetime.time instance. |
URLField | A CharField for a URL, validated by URLValidator. |
UUIDField | A field for storing universally unique identifiers. Uses Python’s UUID class. When used on PostgreSQL, this stores in a uuid datatype, otherwise in a char(32). |
关系字段
Django 还定义了一组表示关系的字段。Field Name Description ForeignKey A many-to-one relationship. Requires two positional arguments: the class to which the model is related and the on_delete option. ManyToManyField A many-to-many relationship. Requires a positional argument: the class to which the model is related, which works exactly the same as it does for ForeignKey, including recursive and lazy relationships. OneToOneField A one-to-one relationship. Conceptually, this is similar to a ForeignKey with unique=True, but the “reverse” side of the relation will directly return a single object.