Commits

Anonymous committed 9c7a217

added improved photo admin for reverse m2m

  • Participants
  • Parent commits b6f98cb
  • Branches cms-trunk

Comments (0)

Files changed (1)

File cmsplugin_photologue/admin.py

+from django.utils.translation import ugettext_lazy as _
+from django.contrib import admin
+from photologue.models import Photo, Gallery
+from photologue.admin import PhotoAdmin
+from django import forms
+
+class PhotoForm(forms.ModelForm):
+
+    class Meta:
+        model = Photo
+
+    galleries = forms.ModelMultipleChoiceField(label=_('Galleries'), queryset=Gallery.objects.all(), required=False, help_text=_('Galleries this photo can be found in'))
+
+class PhotoImprovedAdmin(PhotoAdmin):
+
+    form = PhotoForm
+
+    def save_model(self, request, obj, form, change):  
+        obj.galleries.clear()
+        for gallery in form.cleaned_data['galleries']:
+            obj.galleries.add(gallery)
+        obj.save()
+
+    def get_form(self, request, obj=None, **kwargs):
+        if obj:
+            self.form.base_fields['galleries'].initial = [g.pk for g in obj.galleries.all()]
+        return super(PhotoImprovedAdmin, self).get_form(request, obj) 
+
+
+admin.site.unregister(Photo)
+admin.site.register(Photo, PhotoImprovedAdmin)
+