From: Siobhan Tully Date: Fri, 17 Jan 2014 20:11:14 +0000 (-0500) Subject: Corrected issue with DeploymentAdmin's reverse lookup of Site relationship X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=320b462eb640286897ec1cfef514746212f24d01;p=plstackapi.git Corrected issue with DeploymentAdmin's reverse lookup of Site relationship --- diff --git a/planetstack/core/admin.py b/planetstack/core/admin.py index d11c894..5281bb1 100644 --- a/planetstack/core/admin.py +++ b/planetstack/core/admin.py @@ -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 diff --git a/planetstack/core/models/site.py b/planetstack/core/models/site.py index caf5afb..65d965b 100644 --- a/planetstack/core/models/site.py +++ b/planetstack/core/models/site.py @@ -21,7 +21,7 @@ class Site(PlCoreBase): is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members") abbreviated_name = models.CharField(max_length=80) - deployments = models.ManyToManyField('Deployment', blank=True) + deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites') #deployments = models.ManyToManyField('Deployment', through='SiteDeployments', blank=True) tags = generic.GenericRelation(Tag)