ImageField¶ class ImageField(upload_to=None, height_field=None, width_field=None, max_length=100, **options)¶ Inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image. In addition to the special attributes that are available for FileField, an ImageField also has height and width attributes. To facilitate querying on those attributes, ImageField has two extra optional arguments: ImageField.height_field¶ Name of a model field which will be auto-populated with the height of the image each time the model instance is saved. ImageField.width_field¶ Name of a model field which will be auto-populated with the width of the image each time the model instance is saved. Requires the Pillow library. (pip install Pillow )
ImageField instances are created in your database as varchar columns with a default max length of 100 characters.
As with other fields, you can change the maximum length using the max_length argument. The default form widget for this field is a ClearableFileInput.
FilePathField¶ class FilePathField(path='', match=None, recursive=False, allow_files=True, allow_folders=False, max_length=100, **options)¶ A CharField whose choices are limited to the filenames in a certain directory on the filesystem. Has some special arguments, of which the first is required: # path参数必须是绝对路径,也可以动态拼接 FilePathField.path¶ Required. The absolute filesystem path to a directory from which this FilePathField should get its choices. Example: "/home/images". path may also be a callable, such as a function to dynamically set the path at runtime. Example: import os from django.conf import settings from django.db import models def images_path(): return os.path.join(settings.LOCAL_FILE_DIR, 'images') class MyModel(models.Model): file = models.FilePathField(path=images_path) Changed in Django 3.0: path can now be a callable. # 文件类型匹配 FilePathField.match¶ Optional. A regular expression, as a string, that FilePathField will use to filter filenames. Note that the regex will be applied to the base filename, not the full path. Example: "foo.*.txt$", which will match a file called foo23.txt but not bar.txt or foo23.png. FilePathField.recursive¶ Optional. Either True or False. Default is False. Specifies whether all subdirectories of path should be included FilePathField.allow_files¶ Optional. Either True or False. Default is True. Specifies whether files in the specified location should be included. Either this or allow_folders must be True. FilePathField.allow_folders¶ Optional. Either True or False. Default is False. Specifies whether folders in the specified location should be included. Either this or allow_files must be True. The one potential gotcha is that match applies to the base filename, not the full path. So, this example: FilePathField(path="/home/images", match="foo.*", recursive=True) …will match /home/images/foo.png but not /home/images/foo/bar.png because the match applies to the base filename (foo.png and bar.png). # 默认长度Varchar=100,可自定义修改长度限制 FilePathField instances are created in your database as varchar columns with a default max length of 100 characters. As with other fields, you can change the maximum length using the max_length argument.