/* models.py */ class Post(models.Model): name = models.CharField(max_length=255) description = models.TextField() slug = models.SlugField(max_length=5, unique=True, blank=True, editable=False) slug2 = models.SlugField(max_length=10, blank=True, editable=False) def __str__(self): return self.slug def save(self, *args, **kwargs): if not self.slug: self.slug = get_unique_slug(self.__class__,'slug',self.name) if not self.slug2: self.slug2 = get_unique_slug(self.__class__,'slug2',self.name) super().save(*args, **kwargs) class Image(models.Model): post = models.ForeignKey(Post, related_name='images') file = models.ImageField(upload_to='upload') position = models.PositiveSmallIntegerField(default=0) class Meta: ordering = ['position'] def __str__(self): return '%s - %s ' % (self.post, self.file) /* admin.py */ class ImageInline(admin.StackedInline): model = Image extra = 0 class PostAdmin(admin.ModelAdmin): form = PostForm inlines = [ImageInline] def save_model(self, request, obj, form, change): super(PostAdmin,self).save_model(request, obj, form, change) # obj.save() for afile in request.FILES.getlist('photos_multiple'): obj.images.create(file=afile) /* project ----app_name --------templates ------------admin ----------------app_name --------------------model_name ------------------------change_form.html */ {% extends "admin/change_form.html" %} {% block inline_field_sets %} {% for inline_admin_formset in inline_admin_formsets %} {% include inline_admin_formset.opts.template %} {% endfor %}
{% endblock %}