Image and File fields

I'd like to take a minute to introduce the ImageField model field. This is the first time we are seeing it, and using it is a bit different than other model fields. Here is our Car model that uses this field:

class Car(models.Model):
    name = models.CharField(max_length=100)
    image = models.ImageField(upload_to='car_images')
    description = models.TextField()
    daily_rent = models.IntegerField()

    is_available = models.BooleanField()

    def get_absolute_url(self):
        return reverse('car-details', kwargs={'pk': self.pk})

Note

All of the information in this section about ImageField also relates to FileField.

ImageField is special and different from all the other database model fields that we have looked at for a few reasons. Firstly, it needs ...

Get Django Project Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.