Corrected issue with DeploymentAdmin's reverse lookup of Site relationship
[plstackapi.git] / planetstack / core / admin.py
index d11c894..5281bb1 100644 (file)
@@ -417,6 +417,24 @@ class DeploymentAdminForm(forms.ModelForm):
     class Meta:
         model = Deployment
 
+    def __init__(self, *args, **kwargs):
+      super(DeploymentAdminForm, self).__init__(*args, **kwargs)
+
+      if self.instance and self.instance.pk:
+        self.fields['sites'].initial = self.instance.sites.all()
+
+    def save(self, commit=True):
+      deployment = super(DeploymentAdminForm, self).save(commit=False)
+
+      if commit:
+        deployment.save()
+
+      if deployment.pk:
+        deployment.sites = self.cleaned_data['sites']
+        self.save_m2m()
+
+      return deployment
+
 class SiteAssocInline(PlStackTabularInline):
     model = Site.deployments.through
     extra = 0