The primary key is assigned by the database, so you need to wait until your model string is saved to db.
First, split the data in two models with a thumbnail on the child model:
from django.db import models from .fields import CMSImageField class CMSDocument(models.Model): title = models.CharField(max_length=50) class CMSMediaDocument(CMSDocument): thumb = CMSImageField(upload_to='./media/', blank=True)
As you can see, I use a custom thumbnail field instead of ImageField.
So, create a fields.py file where you must override the pre_save function of the FileField class inherited by ImageField:
from django.db import models class CMSImageField(models.ImageField): def pre_save(self, model_instance, add): file = super(models.FileField, self).pre_save(model_instance, add) if file and not file._committed:
Since CMSMediaDocument is inherited from the CMSDocument class, the moment the pre_save is called, the progressive PK is already stored in the database, so you can extract pk from model_instance.
I tested the code and should work fine.
Administrator file used in the test:
from django.contrib import admin from .models import CMSMediaDocument admin.site.register(CMSMediaDocument)
source share