ImageField - Django Models
Last Updated :
27 May, 2025
ImageField is a specialized version of Django's FileField designed to handle image uploads. It restricts uploads to image formats and provides additional attributes for storing image dimensions.
- Default form widget: ClearableFileInput
- Requires: Pillow library for image processing
Install Pillow with:
pip install Pillow
Syntax
field_name = models.ImageField(
upload_to=None,
height_field=None,
width_field=None,
max_length=100,
**options
)
Key Arguments for ImageField
Argument | Description |
---|
instance | An instance of the model where the ImageField 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:
Python
def user_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT / user_<id>/<filename>
return 'user_{0}/{1}'.format(instance.user.id, filename)
class MyModel(models.Model):
upload = models.ImageField(upload_to = user_directory_path)
height_field and width_field
Names of model fields (usually IntegerFields) that will be automatically populated with the height and width of the uploaded image whenever the model instance is saved.
Example: Using ImageField in a Model
Assuming a Django project geeksforgeeks with an app geeks:
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.
Python
from django.db import models
from django.db.models import Model
class GeeksModel(Model):
geeks_field = models.ImageField()
Setup and Migration
1. Add your app to INSTALLED_APPS in settings.py:
Python
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'geeks',
]
2. Run migrations:
Python manage.py makemigrations
python manage.py migrate
Using ImageField with Django Admin
1. Create a superuser to access the admin interface:
python manage.py createsuperuser
2. Run the development server and log in to the admin:
http://localhost:8000/admin/
3. Add new instances of your model and upload images via the admin panel.

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 for ImageField
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 ImageField will enable it to store empty values for that table in relational database. Here are the field options and attributes that an ImageField 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. |
---|
db_column | The name of the database column to use for this field. If this isn’t given, Django will use the field’s name. |
---|
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. |
---|
primary_key | If True, this field is the primary key for the model. |
---|
editable | If False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True. |
---|
error_messages | The error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override. |
---|
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. |
---|
verbose_name | A human-readable name for the field. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces. |
---|
validators | A list of validators to run for this field. |
---|
Unique | If True, this field must be unique throughout the table. |
---|
Similar Reads
BigIntegerField - Django Models BigIntegerField is a 64-bit integer, much like an IntegerField except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807. The default form widget for this field is a TextInput. Syntax field_name = models.BigIntegerField(**options) Django Model BigIntegerField Expla
4 min read
BinaryField - Django Models BinaryField is a special field to store raw binary data. It can be assigned bytes, bytearray, or memoryview. By default, BinaryField sets editable to False, that is it canât be included in a ModelForm. Since BinaryField stores raw data or in other terms a python object, it can not be manually entere
4 min read
BooleanField - Django Models BooleanField is a true/false field. It is like a bool field in C/C++. The default form widget for this field is CheckboxInput, or NullBooleanSelect if null=True. The default value of BooleanField is None when Field.default isnât defined. One can define the default value as true or false by setting t
4 min read
CharField - Django Models CharField is a string field, for small- to large-sized strings. It is like a string field in C/C++. CharField is generally used for storing small strings like first name, last name, etc. To store larger text TextField is used. The default form widget for this field is TextInput. CharField has one e
4 min read
DateField - Django Models DateField is a field that stores date, represented in Python by a datetime.date instance. As the name suggests, this field is used to store an object of date created in python. The default form widget for this field is a TextInput. The admin can add a JavaScript calendar and a shortcut for âTodayâ e
4 min read
DateTimeField - Django Models DateTimeField is a date and time field which stores date, represented in Python by a datetime.datetime instance. As the name suggests, this field is used to store an object of datetime created in python. The default form widget for this field is a TextInput. The admin uses two separate TextInput wid
5 min read
DecimalField - Django Models DecimalField is a field which stores a fixed-precision decimal number, represented in Python by a Decimal instance. It validates the input using DecimalValidator. Syntax field_name = models.DecimalField(max_digits=None, decimal_places=None, **options) DecimalField has the following required argument
4 min read
DurationField - Django Models DurationField is a field for storing periods of time - modeled in Python by timedelta. When used on PostgreSQL, the data type used is an interval and on Oracle the data type is INTERVAL DAY(9) TO SECOND(6). Otherwise, a bigint of microseconds is used. DurationField basically stores a duration, the d
4 min read
EmailField - Django Models EmailField is a CharField that checks the value for a valid email address using EmailValidator. EmailValidator validates a email through predefined regex which checks '@' and a '.' defined after it. One can change the regex by exploring options from EmailValidator itself. Syntax field_name = models.
4 min read
FileField - Django Models FileField is a file-upload field. Before uploading files, one needs to specify a lot of settings so that file is securely saved and can be retrieved in a convenient manner. The default form widget for this field is a ClearableFileInput. Syntax field_name = models.FileField(upload_to=None, max_length
6 min read