FilePathField is a CharField whose choices are limited to the filenames in a certain directory on the filesystem. FilePathField instances are created in your database as
varchar columns with a default max length of 100 characters.
field_name = models.FilePathField(path=None, match=None, recursive=False, max_length=100, **options)
FilePathField has following special arguments, of which the first is required:
- FilePathField.path – This attribute is required. The absolute filesystem path to a directory from which this FilePathField should get its choices. Example:
- FilePathField.match – 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 – Either True or False. Default is False. Specifies whether all subdirectories of path should be included
- FilePathField.allow_files – Either True or False. Default is True. Specifies whether files in the specified location should be included. Either this or
allow_foldersmust be True.
- FilePathField.allow_folders – Either True or False. Default is False. Specifies whether folders in the specified location should be included. Either this or
allow_filesmust be True.
The one major point to consider is that match applies to the base filename, not the full path. So, this example:
…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).
Django Model FilePathField Explanation
Illustration of FilePathField using an Example. Consider a project named
geeksforgeeks having an app named
Refer to the following articles to check how to create a project and an app in Django.
Enter the following code into
models.py file of geeks app.
Add the geeks app to
Now when we run
makemigrations command from the terminal,
Python manage.py makemigrations
A new folder named migrations would be created in
geeks directory with a file named
Python manage.py migrate
geeks_field FilePathField is created when you run migrations on the project. It is a field to store any kind of file in the database.
How to use FilePathField ?
FilePathField is used for storing files into the database. One can any type of file in FilePathField. Let’s try storing an image in the model created above.
- To start creating instances of model, create an admin account by using the following command.
Python manage.py createsuperuser
- Enter a username, email and a secure password. Then in your browser enter the following URL.
- Go to add in front of Geeks Models.
Choose the file you want to upload and click on save. Now let’s check it in admin server. We have created an instance of GeeksModel.
Field Options are the arguments given to each field for applying some constraint or imparting a particular characteristic to a particular Field. For example, adding an argument
null = True to FilePathField will enable it to store empty values for that table in relational database.
Here are the field options and attributes that a FilePathField can use.
|Null||If True, Django will store empty values as NULL in the database. Default is False.|
|Blank||If True, the field is allowed to be blank. Default is False.|
|Default||The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.|
|help_text||Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form.|
|Unique||If True, this field must be unique throughout the table.|
- FilePathField - Django Forms
- Django Models | Set - 1
- Django Models
- Django Models | Set - 2
- FloatField - Django Models
- BinaryField - Django Models
- EmailField - Django Models
- FileField - Django Models
- DurationField - Django Models
- DecimalField - Django Models
- BigIntegerField - Django Models
- AutoField - Django Models
- BigAutoField - Django Models
- BooleanField - Django Models
- CharField - Django Models
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.