Django application for image assets management.
- There are several content types on a web site
- Each of them has a set of required or additional image assets
- Every asset of same asset type must satisfy custom constraints on dimensions, format and file size.
- The most important thing: these constraints and required asset type sets are updated often, along with web design evolves and more platforms are added.
pip install django-image-assets
Working example is in testproject.testapp
.
-
Add
image_assets
application to installed apps in django settings:INSTALLED_APPS.append('image_assets')
-
Add generic relation to your content models:
from django.contrib.contenttypes.fields import GenericRelation from django.db import models from image_assets.models import Asset class Video(models.Model): assets = GenericRelation(Asset, blank=True)
-
Setup inlines for assets
from django.contrib import admin from image_assets.admin import AssetsInline from testproject.testapp import models @admin.register(models.Video) class VideoAdmin(admin.ModelAdmin): inlines = (AssetsInline,)
- Create new asset type (i.e. "thumbnail")
- Add
Video
toallowed_for
set: now you can add a thumbnail to a video. Or you may skip this asset. - Add
Article
torequired_for
set: now you will able to create or edit an article with valid "thumbnail" asset only. - When an asset is deleted, it's file is owned by
DeletedAsset
object and may be wiped later by manual or automatic cleanup.
If you need to alter model fields i.e. for AssetType
, you may subclass
existing model and than change image_assets application settings.
- Subclass
AssetType
modelfrom django.db import models from image_assets.models import AssetType class MyAssetType(AssetType): some_feature_flag = models.BooleanField(default=False)
- Change a reference to an asset type model in settings:
IMAGE_ASSETS_CONFIG = { 'ASSET_TYPE_MODEL': 'my_app.MyAssetType', 'ASSET_MODEL': 'image_assets.Asset', 'DELETED_ASSET_MODEL': 'image_assets.DeletedAsset' }
image_assets.AssetType
will be declared as abstract andMyAssetType
will be returned as result ofimage_assets.models.get_asset_type_model()