Search code examples
pythondjangodjango-filer

django-filer uploads automatically create instances of a model


Here is myapp.model

from django.db import models
from filer.fields.image import FilerImageField

class Item(models.Model):
    ...
    image = FilerImageField()
    ...

I'd like to hack in the middle of the django-filer uploading process and autocreate instances of Item for every image django-filer receives.

django-filer doesn't have traditional urls.py for me to just override single url pattern pointing to a custom view. How can I approach this?

EDIT:

Thanks to a hint from stefanfoulis, I eventually end up with this code:

from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from filer.fields.image import FilerImageField
from filer.models import Image

class Item(models.Model):
    ...
    image = FilerImageField()
    ...


@receiver(post_save, sender=Image)
def filer_signal(sender, instance, created, **kwargs):
    Item.objects.create(
        ...
        image=instance,
        ...).save()
    return

Solution

  • Files in django-filer are just regular models. FilerImageField is a ForeignKey to filer.models.Image under the hood. So you can listen to the post_save signal of the File or Image model and create your instance there.

    Signal docs: https://docs.djangoproject.com/en/dev/ref/signals/#post-save