Custom Django Inline Admin

2019-05-18 19:08发布

问题:

Let's say I have these two models:

class Egg(models.Model):
    # some fields

class Spam(models.Model):
    egg = models.ForeignKey(Egg)
    img = models.ImageField()

I planned to have the spams inlined to egg in admin site. The problem is I also want a very customized method on uploading the spam images (like this), like having my own view and template. So far, I just got :

class CustomInline(admin.StackedInline):
    model = Spam
    template = 'admin/app/inline.html' # empty

class EggAdmin(admin.ModelAdmin):
    inlines = [CustomInline, ]

The idea is having some kind of gallery of spams and custom image upload in egg admin. (Is this achievable?)

So the questions are:

  1. I want to injects variables to be available on the template (spam objects on inline.html for gallery) . Is there a way to do this?
  2. Is it okay to POST something to a view (upload process)? Or that particular view must be registered first on admin site or something?

I've looked on InlineAdmin source but still have no idea what to do/override

Thanks

回答1:

using the form property you can subclass your ModelForm and completely change the way your inline form works.