4.7. Models Field Storage
models.ImageField
- stores an image, in database it stores the path to the imagemodels.FileField
- stores a file, in database it stores the path to the filemodels.FilePathField
- stores a file path, in database it stores the path to the filemodels.BinaryField
- stores a binary data, in database it stores the binary data (BLOB)
4.7.1. SetUp
>>> from django.db import models
>>> from django.utils.translation import gettext_lazy as _
4.7.2. Arguments
upload_to
(str) - The directory to which the file is uploaded.blank
choices
db_column
db_index
default
editable
error_message
help_text
limit_choices_to
max_length
null
primary_key
unique
validators
verbose_name
4.7.3. ImageField
ImageField
pip install Pillow
settings.MEDIA_ROOT
settings.MEDIA_URL
>>> image = models.ImageField(
... verbose_name=_('Image'),
... upload_to='images/',
... null=True,
... blank=True,
... default=None,
... )
4.7.4. FileField
FileField
settings.MEDIA_ROOT
settings.MEDIA_URL
>>> file = models.FileField(
... verbose_name=_('File'),
... upload_to='files/',
... null=True,
... blank=True,
... default=None,
... )
4.7.5. FilePathField
FilePathField
4.7.6. BinaryField
BinaryField
4.7.7. Use Case - 1
>>>
... from django.db import models
... from django.utils.translation import gettext_lazy as _
...
...
... class Customer(models.Model):
... firstname = models.CharField(verbose_name=_('First Name'), max_length=50)
... lastname = models.CharField(verbose_name=_('Last Name'), max_length=50, db_index=True)
... image = models.ImageField(verbose_name=_('Image'), upload_to=upload_path, null=True, blank=True, default=None)
... diploma = models.FileField(verbose_name=_('Diploma'), upload_to='customer/', null=True, blank=True, default=None)
...
... def __str__(self):
... return f'{self.firstname} {self.lastname}'
...
... class Meta:
... verbose_name = _('Customer')
... verbose_name_plural = _('Customers')
4.7.8. Assignments
# TODO: Create Tests
# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Django Model Storage
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `file` as `models.FileField`:
# - verbose_name=_('File')
# - upload_to='files/'
# - null=True
# - blank=True
# - default=None
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`
# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Person`
# 2. Dodaj pole `file` jako `models.FileField`:
# - verbose_name=_('File')
# - upload_to='files/'
# - null=True
# - blank=True
# - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `Add field file to person`
# - `Applying demo.0007_person_file... OK`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
from django.db import models
from django.utils.translation import gettext_lazy as _
class Person(models.Model):
file = ...
# TODO: Create Tests
# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Django Model Storage
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `image` as `models.ImageField`:
# - verbose_name=_('Image')
# - upload_to='images/'
# - null=True
# - blank=True
# - default=None
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`
# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Person`
# 2. Dodaj pole `image` jako `models.ImageField`:
# - verbose_name=_('Image')
# - upload_to='images/'
# - null=True
# - blank=True
# - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`
# %% Output
# - `Add field image to person`
# - `Applying demo.0007_person_image... OK`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
from django.db import models
from django.utils.translation import gettext_lazy as _
class Person(models.Model):
image = ...