Добавлено API для получения списка атрибутов и их значений; обновлены формы для работы с атрибутами через JavaScript

This commit is contained in:
2025-12-30 02:41:30 +03:00
parent f39ee5f15d
commit a3f2185714
6 changed files with 498 additions and 46 deletions

View File

@@ -102,6 +102,7 @@ from .attribute_views import (
create_attribute_api,
add_attribute_value_api,
delete_attribute_value_api,
get_attributes_list_api,
)
# API представления
@@ -193,6 +194,7 @@ __all__ = [
'create_attribute_api',
'add_attribute_value_api',
'delete_attribute_value_api',
'get_attributes_list_api',
# API
'search_products_and_variants',

View File

@@ -245,3 +245,30 @@ def delete_attribute_value_api(request, pk, value_id):
return JsonResponse({'success': False, 'error': 'Значение не найдено'})
except Exception as e:
return JsonResponse({'success': False, 'error': str(e)})
@login_required
def get_attributes_list_api(request):
"""
API для получения списка всех атрибутов с их значениями.
Используется для autocomplete в форме создания вариативного товара.
"""
attributes = ProductAttribute.objects.prefetch_related('values').order_by('position', 'name')
data = []
for attr in attributes:
data.append({
'id': attr.pk,
'name': attr.name,
'slug': attr.slug,
'values': [
{
'id': val.pk,
'value': val.value,
'slug': val.slug
}
for val in attr.values.all().order_by('position', 'value')
]
})
return JsonResponse({'success': True, 'attributes': data})

View File

@@ -13,7 +13,7 @@ from django.contrib.auth.decorators import login_required
from django.db import transaction
from user_roles.mixins import ManagerOwnerRequiredMixin
from ..models import ConfigurableProduct, ConfigurableProductOption, ProductKit, ConfigurableProductAttribute
from ..models import ConfigurableProduct, ConfigurableProductOption, ProductKit, ConfigurableProductAttribute, ProductAttribute
from ..forms import (
ConfigurableProductForm,
ConfigurableProductOptionFormSetCreate,
@@ -144,6 +144,9 @@ class ConfigurableProductCreateView(LoginRequiredMixin, ManagerOwnerRequiredMixi
is_temporary=False
).order_by('name')
# Справочник атрибутов для autocomplete
context['product_attributes'] = ProductAttribute.objects.prefetch_related('values').order_by('position', 'name')
return context
def form_valid(self, form):
@@ -420,6 +423,9 @@ class ConfigurableProductUpdateView(LoginRequiredMixin, ManagerOwnerRequiredMixi
is_temporary=False
).order_by('name')
# Справочник атрибутов для autocomplete
context['product_attributes'] = ProductAttribute.objects.prefetch_related('values').order_by('position', 'name')
return context
def form_valid(self, form):