I am having problems saving a MultipleChoiceField
on my test server (with Apache). However, it works on my local machine (django server).
In a form, I have checkboxes
that I can check and uncheck. When I click on a save button, the data related to the checkboxes are saved in the database and the form is reloaded and the checkboxes updated.
However, this is how it works in local but not on the test server. On the test server, when I click on the save button, it just reloads the form, nothing is saved, nothing is changed.
Here is the code:
class Participant(models.Model):
databases = models.ManyToManyField(Advertiser, null=True, blank=True, through='ShareDataToBrands')
@property
def share_to_brands_list(self):
brands=[]
for brand in ShareDataToBrands.objects.all():
brands.append((brand.advertiser.id, brand.advertiser.brand_name, brand.share_data))
return brands
class ShareDataToBrandsForm(forms.ModelForm):
class Meta:
model = models.Participant
fields = ('databases', )
databases=forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, required=False)
def save(self, *args, **kwargs):
instance=super(ShareDataToBrandsForm, self).save(commit=False)
#list of brands to share data with
share_list=map(int, self.cleaned_data.get("databases"))
participant=self.instance
for share_data_instance in models.ShareDataToBrands.objects.filter(participant=participant):
if share_data_instance.advertiser.id in share_list:
share_data=True
else:
share_data=False
#update record
share_data_instance.share_data=share_data
share_data_instance.save()
return instance
What could possibly be wrong?
EDIT :
When I check the log
, I see that the program never enters in the for loop
! However, I have records in the database matching the filter.
I have found the solution, looping through theShareDataToBrands
instances related to the participant instead of all the ShareDataToBrands
instances.
In the share_to_brands_list
property, have changed the following line:
for brand in ShareDataToBrands.objects.all():
with
for brand in ShareDataToBrands.objects.filter(participant=self):