Common data types of Django models


CharField
class CharField(max_length=None[, **options])

#String (stores various lengths from small to large) 
#If it is a huge text type, you can use TextField, the default form style of this field is TextInput, CharField must receive an additional parameter: max_length, in the database layer and Django form validation Works in, used to limit the length of the field
IntegerField
class IntegerField([**options])


#Integer ( -2147483648 to 2147483648) #The default form input tool is TextInput
FloatField
class FloatField([**options])

#Float number # max_digits 
Total digits (excluding decimal point and sign) 
# decimal_places Decimal digits 
# For example, to save the maximum value as 999 (2 digits after the decimal point), you need to define the field like this: 
# models.FloatField(.. ., max_digits=5, decimal_places=2)
AutoField
class AutoField(**options)

#IntegerField that automatically grows according to the actual ID #It 
will grow automatically when you add a record, you usually don't need to use this field directly 
#Customize a primary key: my_id=models.AutoField(primary_key=True), if you don't specify a primary key, the system will automatically add a primary key field to your model.
BooleanField
class BooleanField(**options)

# true/false 
#The default form widget for this field is a CheckboxInput, if you need to set a null value, use NullBooleanField instead of BooleanField. The default value of BooleanField is None if Field.default is not specified.
TextField
class TextField([**options])


#Large text field # The default form component of this model is Textarea
EmailField
class EmailField([max_length=254, **options])

#CharField with checking Email validity 
#It uses EmailValidator to verify input validity
DateField
class DateField([auto_now=False, auto_now_add=False, **options])

# DateField 
# Argument Description 
# auto_now Automatically sets the value of this field to the current time when the object is saved, usually used to represent a "last-modified" timestamp 
# auto_now_add Automatically sets the value of this field to the current time when the object is first created The value is set to the current time, usually used to represent the object creation time
DateTimeField
class DateTimeField([auto_now=False, auto_now_add=False, **options])

# DateTimeField 
# Similar to DateField with the same additional options
ImageField
class ImageField([upload_to=None, height_field=None, width_field=None, max_length=100, **options])

#Similar to FileField, but to check whether the uploaded object is a valid image 
#It has two optional parameters: height_field and width_field, if these two parameters are provided, the image will be saved according to the provided height and width specifications
FileField
class FileField([upload_to=None, max_length=100, **options])

#The field of the uploaded file 
# The field does not support the primary_key and unique parameters. If used, it will generate a TypeError error. In the old version of Django, the upload_to attribute is required
URLField
class URLField([max_length=200, **options])

# URL of type CharField 
# If the verify_exists parameter is True (default), the given URL is pre-checked for existence (i.e. the URL is loaded effectively and no 404 response is returned)
SlugField
class SlugField([max_length=50, **options])


#short title # A slug can only contain letters, numbers, underscores or hyphens, usually used as short tags, usually they are used in URLs. Similar to CharField, you can specify the value of max_length, if max_length is not specified, Django will default the length to 50
FilePathField
class FilePathField(path=None[, match=None, recursive=False, max_length=100, **options])

#A CharField, the content is limited to the file name in a specific directory in the file system #Parameters, 
the first of which is required: 
# path Required. This FilePathField should get the absolute filesystem path of the directory it selects. For example: "/home/images" 
# match optional, match only matches the base filename (base filename), not the full file path (full path) 
# recursive optional, True/False, default False 
# allow_files optional, True /False, default False 
# allow_folders optional, True/False, default False
GenericIPAddressField
class GenericIPAddressField([protocol=both, unpack_ipv4=False, **options])

# IPv4 or IPv6 address, in string format (eg 192.0.2.30 or 2a02:42fe::4) 
# The default form widget is a TextInput
CommaSeparatedIntegerField
class CommaSeparatedIntegerField(max_length=None[, **options])

# Comma-separated integer fields 
# Like CharField, it needs a max_length parameter, and you also need to pay attention to database migration
BigIntegerField
class BigIntegerField([**options])

# 64-bit integer 
# Similar to an IntegerField, its value range is -9223372036854775808 to 9223372036854775807, the default form component of this field is a TextInput
BinaryField
class BinaryField([**options])


#Store the original binary code # Only support bytes assignment, note that this Field has only very limited functions
PositiveIntegerField
class PositiveIntegerField([**options])

#Similar to IntegerField, but the value must be positive or zero (0)
UUIDField
class UUIDField([**options])

#Store UUID #Use Python 's UUID class 
. When using a PostgreSQL database, the data type in the database corresponding to this field type is uuid, and when using other databases, the database corresponds to the char(32) type. Using the UUID type is a better solution than using the AutoField type with the primary_key parameter. The database does not automatically generate UUID, so it is recommended to use the default parameter: 
import uuid
 from django.db import models
 class MyUUIDModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
 


Reference articles:
1. https://www.cnblogs.com/laoguiaabb/p/8182906.html
2. https://my.oschina.net/liuyuantao/blog/751343

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326172919&siteId=291194637