Last Updated : 12 Jul, 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.
Install Pillow with:
Syntaxpip install Pillow
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 pathfield_name = models.ImageField(
upload_to=None,
height_field=None,
width_field=None,
max_length=100,
**options
)
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 ModelAssuming 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:
Using ImageField with Django AdminPython manage.py makemigrations
python manage.py migrate
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 ImageFieldField 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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4