Model IntegrationΒΆ
The centerpiece of django-versatileimagefield is its VersatileImageField which provides a simple, flexible interface for creating new images from the image you assign to it.
VersatileImageField extends django’s ImageField and can be used as a drop-in replacement for it. Here’s a simple example model that depicts a typical usage of django’s ImageField:
# models.py with `ImageField`
from django.db import models
class ImageExampleModel(models.Model):
name = models.CharField(
'Name',
max_length=80
)
image = models.ImageField(
'Image',
upload_to='images/testimagemodel/',
width_field='width',
height_field='height'
)
height = models.PositiveIntegerField(
'Image Height',
blank=True,
null=True
)
width = models.PositiveIntegerField(
'Image Width',
blank=True,
null=True
)
class Meta:
verbose_name = 'Image Example'
verbose_name_plural = 'Image Examples'
And here’s that same model using VersatileImageField instead (see highlighted section in the code block below):
# models.py with `VersatileImageField`
from django.db import models
from versatileimagefield.fields import VersatileImageField
class ImageExampleModel(models.Model):
name = models.CharField(
'Name',
max_length=80
)
image = VersatileImageField(
'Image',
upload_to='images/testimagemodel/',
width_field='width',
height_field='height'
)
height = models.PositiveIntegerField(
'Image Height',
blank=True,
null=True
)
width = models.PositiveIntegerField(
'Image Width',
blank=True,
null=True
)
class Meta:
verbose_name = 'Image Example'
verbose_name_plural = 'Image Examples'
Note
VersatileImageField is fully interchangable with django.db.models.ImageField which means you can revert back to using django’s ImageField anytime you’d like. It’s fully-compatible with south so migrate to your heart’s content!