ImageField — Django Models

Python Methods and Functions

ImageField — this is a ** options )

ImageField has the following optional arguments:

ImageField.upload_to 

This attribute provides a way to set the download directory and filename, and can be set 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.ImageField (upload_to = `uploads /` )

 

# or ...

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

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

If you are using FileSystemStorage By default, a string value will be added to your MEDIA_ROOT path to form the location on the local filesystem 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 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:

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. ImageField (upload_to = user_directory_path)

ImageField.height_field

The name of the model field, which will be automatically filled with the image height each time the model is saved.

 ImageField.width_field 

Name of the model field that will be automatically captured Fill in the width of the image each time the model is saved.

Django ImageField Model Explanation

ImageField illustration using an example. 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 models.py 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.ImageField ()

# Application definition

 

INSTALLED_APPS = [

`django.contrib.admin` ,

  ` django.contrib.auth` ,

`django.contrib.contenttypes` ,

`django.contrib.sessions` ,

`django.contrib.messages` ,

`django.contrib.staticfiles ` ,

  ` geeks` ,

]

Now when we run the makemigrations command from the terminal,

 Python manage.py makemigrations  

In the geeks directory < / code> a new folder named migrations will be created with a file named 0001_initial.py

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

 

from django.db import migrations, models

 

class Migration (migrations.Migration):

 

initial = True

 

dependencies = [

]

 

operations = [

migrations.CreateModel (

  name = `GeeksModel` ,

  fields = [

( `id`

models.AutoField (

aut o_created = True ,

primary_key = True ,

serialize = False

  verbose_name = ` ID`

)),

( `geeks_field` , models.ImageField ()),

],

),

]

Now run

 Python manage.py migrate 

So geeks_field ImageField is generated when you run migrations in the project. This is a field to store valid image files.




How to use ImageField?

ImageField is used to store valid image files in the database. Can any type of image file in ImageField. 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 particular characteristic to a particular field. For example, adding the null = True argument to the ImageField will allow it to store empty values ​​for that table in a relational database. 
Here are the field options and attributes that 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.
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.





Get Solution for free from DataCamp guru