Looking to see if anyone has come up with a creative approach for storing SVG files in an ImageField.
Out of the box django doesn't support storing of SVG files in an ImageField. There's a (a few) hack(s) out there that allow the SVGs to be stored BUT the specific issue I'm encountering is that when an object with an SVG in an ImageField is retrieved from the database, if the respective file is not present in storage (for any number of reasons) an IOError is raised when trying to update the dimensions(here). This is really not ideal behavior. Ideally, django would offer some option to prevent the updating of the dimension fields, but this does not appear to be available.
For context, consider an application where a user need to upload a logo. It's a bit strained to maintain separate models for JPEG or PNG files and SVG files. For DRF projects (which I am), perhaps there's a proxy model solution here using a serializer with a dynamic model.
class PublicImage(IsSVGMixin, SomeModelMixin):
id = models.UUIDField(primary_key=True,
default=uuid.uuid4,
editable=False)
image = models.ImageField(width_field='width',
height_field='height',
upload_to='public_images')
width = models.IntegerField(null=True, blank=True)
height = models.IntegerField(null=True, blank=True)