With the help from lazerscience and this post I ended up with the following.
The ModelAdmin:
class GalleryAdmin(admin.ModelAdmin):
form = GalleryForm
def __init__(self, model, admin_site):
self.form.admin_site = admin_site
super(GalleryAdmin, self).__init__(model, admin_site)
And my form:
class GalleryForm(ModelForm):
photos = ThumbnailChoiceField(queryset=Photo.objects.all(), label='Photos', widget=MyWidget(), required=False)
def __init__(self, *args, **kwargs):
super(GalleryForm, self).__init__(*args, **kwargs)
rel = ManyToOneRel(self.instance.photos.model, 'id')
self.fields['photos'].widget = RelatedFieldWidgetWrapper(self.fields['photos'].widget, rel, self.admin_site)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…