Don't save ProductAttributeValue instances if value is an empty queryset or empty list if attribute.type==MULTI_OPTION
@@ -755,6 +755,10 @@ class AbstractProductAttribute(models.Model):
def is_option(self):
return self.type == self.OPTION
+ @property
+ def is_multi_option(self):
+ return self.type == self.MULTI_OPTION
+
@property
def is_file(self):
return self.type in [self.FILE, self.IMAGE]
@@ -788,6 +792,20 @@ class AbstractProductAttribute(models.Model):
# New uploaded file
value_obj.value = value
value_obj.save()
+ elif self.is_multi_option:
+ # ManyToMany fields are handled separately
+ if value is None:
+ value_obj.delete()
+ return
+ try:
+ count = value.count()
+ except (AttributeError, TypeError):
+ count = len(value)
+ if count == 0:
+ value_obj.delete()
+ else:
+ value_obj.value = value
+ value_obj.save()
else:
if value is None or value == '':
value_obj.delete()
