5.7. Models Field Storage

  • models.ImageField - stores an image, in database it stores the path to the image

  • models.FileField - stores a file, in database it stores the path to the file

  • models.FilePathField - stores a file path, in database it stores the path to the file

  • models.BinaryField - stores a binary data, in database it stores the binary data (BLOB)

>>> 
... 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')

5.7.1. 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