FileField — Django Models


 field_name = models.FileField (upload_to = None, max_length = 254,  ** options  ) 

FileField has optional arguments:


This attribute provides a way to set the upload directory and file name, and can be installed in two ways. In both cases, the value is passed to the Storage method .save () . If you supply a string value, it can contain the formatting strftime () , which will be replaced by the date / time of the file upload (so that the uploaded files do not fill this directory). For example:

class MyModel (models.Model):


# the file will be uploaded to MEDIA_ROOT / uploads

upload = models.FileField (upload_to = `uploads /` )


# or ...

# the file will be saved to MEDIA_ROOT / uploads / 2015/01/30

upload  = models.FileField (upload_to = `uploads /% Y /% m /% d / ` )

If you are using FileSystemStorage By default, a string value will be appended to your MEDIA_ROOT path to form the location on the local file system where the downloaded files will be stored. If you are using another repository, check the documentation for that repository to see how it handles upload_to .

upload_to can also be callable by, for example, a function. This will be called to get the download path including the filename. This callable must take two arguments and return a Unix-style path (with a forward slash) to pass to the storage system. Two arguments:

Argument Description
instance An instance of the model where the FileField is defined. More specifically, this is a particular instance where the current file is being attached.
filename The filename that was originally given to the file. This may or may not be taken into account when determining the final destination path

For example:

def user_directory_path (instance, filename):


# the file will be uploaded to MEDIA_ROOT / user_ & lt; id & gt; / & lt; filename & gt;

return `user_ {0} / {1}` . format (instance .user. id , filename)


class MyModel (models.Model):

upload = models. FileField (upload_to = user_directory_path)

Django Model FileField Explanation

Example FileField illustration. Consider a project named pythonengineering that has an application named geeks .

Refer to the following articles to check how to create a project and an app in Django.

Enter the following code into the file of the geeks application.

Add the geek app to INSTALLED_APPS

from django.db import models

from django.db.models import Model

# Create your models here.


class GeeksModel (Model):

geeks_field = models.FileField ()

# Application definition



  ` django .contrib.admin` , < / code>

`django.contrib.auth` ,

`django.contrib.contenttypes` ,

  ` django.contrib.sessions` ,

`django.contrib.messages` ,

`django.contrib.staticfiles` ,

`geeks` ,


Now when we run makemigrations from the terminal,

 Python makemigrations 

In the geeks directory b A new folder named migrations will be created with a file named

# Generated by Django 2.2.5 on 2019-09- 06:00 25


from django.db import migrations, models


class Migration (migrations.Migration):


initial = True


dependencies = [



operations = [

migrations.CreateModel (

name = `GeeksModel` ,

  fields = [

( `id`

models.AutoField (

  auto_create d = True ,

primary_key = True ,

serialize = False

  verbose_name = ` ID`


( `geeks_field` , models.FileField ()),





Now run,

 Python migrate 

So geeks_field geeks_field is created when you run migrations in the project. This field is for storing any type of file in the database.

How to use FileField?

FileField is used to store files in the database. Any type of file in FileField can be used. Let`s try to save the image in the model created above.

Field options

Field parameters — they are arguments given to each field to apply some constraint or to convey a specific characteristic to a particular field. For example, adding the null = True argument to the FileField will allow it to store empty values ​​for that table in a relational database. 
Here are the field options and attributes that FileField can use.

Field Options Description
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.