def create(self, validated_data): existing_option = find_existing_attribute_option_group( validated_data["name"], validated_data["options"]) if existing_option is not None: return existing_option else: options = validated_data.pop("options", None) instance = super(AttributeOptionGroupSerializer, self).create(validated_data) options = bound_unique_together_get_or_create_multiple( instance.options, options) instance.options.set(options) return instance
def update(self, instance, validated_data): existing_option = find_existing_attribute_option_group( validated_data["name"], validated_data["options"]) if existing_option is not None: return existing_option else: options = validated_data.pop("options", None) updated_instance = super(AttributeOptionGroupSerializer, self).update(instance, validated_data) # if the field was returned unbound, options = bound_unique_together_get_or_create_multiple( updated_instance.options, options) updated_instance.options.set(options, bulk=False) if not self.partial: # we need to manually remove the options updated_instance.options.exclude( pk__in=[o.pk for o in options]).delete() return updated_instance