def save_paired_items(request, user, formset, Model,
                      item_name, counterpart_name):
    """
    Handle saving skills or links to the database.
    """
    paired_items = []

    for form in formset:
        if form.is_valid():
            item = form.cleaned_data.get(item_name, None)
            counterpart = form.cleaned_data.get(counterpart_name, None)

            if item and counterpart:
                model_instance = Model(user=user)
                setattr(model_instance, item_name, item)
                setattr(model_instance, counterpart_name, counterpart)
                paired_items.append(model_instance)

    # Replace old pairs with new
    # Do this in a transaction to avoid a case where we delete the old
    # but cannot save the new
    try:
        with transaction.atomic():
            Model.objects.filter(user=user).delete()
            Model.objects.bulk_create(paired_items)
    except IntegrityError:
        messages.error(request, _('There was an error updating your profile.'))
        return redirect(reverse('accounts:profile-settings'))