|
|
@@ -2,37 +2,144 @@ from django.contrib import admin
|
|
|
from .models import Product, ProductType, ProductAttribute, AttributePossibleValue
|
|
|
|
|
|
|
|
|
+# @admin.register(Product)
|
|
|
+# class ProductAdmin(admin.ModelAdmin):
|
|
|
+# list_display = ('item_id', 'product_name', 'product_type', 'image_path')
|
|
|
+# search_fields = ('item_id', 'product_name', 'product_type')
|
|
|
+# list_filter = ('product_type',)
|
|
|
+# readonly_fields = ('image_path',)
|
|
|
+# ordering = ('product_name',)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class AttributePossibleValueInline(admin.TabularInline):
|
|
|
+ model = AttributePossibleValue
|
|
|
+ extra = 1
|
|
|
+
|
|
|
+
|
|
|
+# @admin.register(ProductAttribute)
|
|
|
+# class ProductAttributeAdmin(admin.ModelAdmin):
|
|
|
+# list_display = ('name', 'product_type', 'is_mandatory')
|
|
|
+# list_filter = ('product_type', 'is_mandatory')
|
|
|
+# search_fields = ('name', 'product_type__name')
|
|
|
+# inlines = [AttributePossibleValueInline]
|
|
|
+
|
|
|
+
|
|
|
+# @admin.register(AttributePossibleValue)
|
|
|
+# class AttributePossibleValueAdmin(admin.ModelAdmin):
|
|
|
+# list_display = ('attribute', 'value')
|
|
|
+# search_fields = ('attribute__name', 'value')
|
|
|
+# list_filter = ('attribute__product_type',)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+# admin.py
|
|
|
+from django.contrib import admin
|
|
|
+from .models import Product, ProductType, ProductAttribute, AttributePossibleValue, ProductAttributeValue
|
|
|
+
|
|
|
+
|
|
|
+@admin.register(ProductAttributeValue)
|
|
|
+class ProductAttributeValueAdmin(admin.ModelAdmin):
|
|
|
+ """Admin interface for managing original attribute values."""
|
|
|
+ list_display = ['item_id_display', 'attribute_name', 'original_value', 'updated_at']
|
|
|
+ list_filter = ['attribute_name', 'created_at', 'updated_at']
|
|
|
+ search_fields = ['product__item_id', 'product__product_name', 'attribute_name', 'original_value']
|
|
|
+ readonly_fields = ['created_at', 'updated_at']
|
|
|
+
|
|
|
+ fieldsets = (
|
|
|
+ ('Product Information', {
|
|
|
+ 'fields': ('product',)
|
|
|
+ }),
|
|
|
+ ('Attribute Details', {
|
|
|
+ 'fields': ('attribute_name', 'original_value')
|
|
|
+ }),
|
|
|
+ ('Timestamps', {
|
|
|
+ 'fields': ('created_at', 'updated_at'),
|
|
|
+ 'classes': ('collapse',)
|
|
|
+ }),
|
|
|
+ )
|
|
|
+
|
|
|
+ def item_id_display(self, obj):
|
|
|
+ return obj.product.item_id
|
|
|
+ item_id_display.short_description = 'Item ID'
|
|
|
+ item_id_display.admin_order_field = 'product__item_id'
|
|
|
+
|
|
|
+ def get_queryset(self, request):
|
|
|
+ """Optimize queries by selecting related product."""
|
|
|
+ return super().get_queryset(request).select_related('product')
|
|
|
+
|
|
|
+
|
|
|
+class ProductAttributeValueInline(admin.TabularInline):
|
|
|
+ """Inline admin for showing attribute values in Product admin."""
|
|
|
+ model = ProductAttributeValue
|
|
|
+ extra = 1
|
|
|
+ fields = ['attribute_name', 'original_value', 'updated_at']
|
|
|
+ readonly_fields = ['updated_at']
|
|
|
+
|
|
|
+
|
|
|
@admin.register(Product)
|
|
|
class ProductAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('item_id', 'product_name', 'product_type', 'image_path')
|
|
|
- search_fields = ('item_id', 'product_name', 'product_type')
|
|
|
- list_filter = ('product_type',)
|
|
|
- readonly_fields = ('image_path',)
|
|
|
- ordering = ('product_name',)
|
|
|
+ """Enhanced Product admin with inline attribute values."""
|
|
|
+ list_display = ['item_id', 'product_name', 'product_type', 'attribute_count']
|
|
|
+ list_filter = ['product_type']
|
|
|
+ search_fields = ['item_id', 'product_name', 'product_type']
|
|
|
+ inlines = [ProductAttributeValueInline]
|
|
|
+
|
|
|
+ fieldsets = (
|
|
|
+ ('Basic Information', {
|
|
|
+ 'fields': ('item_id', 'product_name', 'product_type')
|
|
|
+ }),
|
|
|
+ ('Descriptions', {
|
|
|
+ 'fields': ('product_short_description', 'product_long_description'),
|
|
|
+ 'classes': ('collapse',)
|
|
|
+ }),
|
|
|
+ ('Media', {
|
|
|
+ 'fields': ('image_path', 'image')
|
|
|
+ }),
|
|
|
+ )
|
|
|
+
|
|
|
+ def attribute_count(self, obj):
|
|
|
+ """Display count of original attribute values."""
|
|
|
+ return obj.attribute_values.count()
|
|
|
+ attribute_count.short_description = 'Original Attributes'
|
|
|
+
|
|
|
+ def get_queryset(self, request):
|
|
|
+ """Optimize queries."""
|
|
|
+ return super().get_queryset(request).prefetch_related('attribute_values')
|
|
|
|
|
|
|
|
|
@admin.register(ProductType)
|
|
|
class ProductTypeAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('name',)
|
|
|
- search_fields = ('name',)
|
|
|
- ordering = ('name',)
|
|
|
-
|
|
|
-
|
|
|
-class AttributePossibleValueInline(admin.TabularInline):
|
|
|
- model = AttributePossibleValue
|
|
|
- extra = 1
|
|
|
+ list_display = ['name', 'attribute_count']
|
|
|
+ search_fields = ['name']
|
|
|
+
|
|
|
+ def attribute_count(self, obj):
|
|
|
+ return obj.attributes.count()
|
|
|
+ attribute_count.short_description = 'Attributes'
|
|
|
|
|
|
|
|
|
@admin.register(ProductAttribute)
|
|
|
class ProductAttributeAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('name', 'product_type', 'is_mandatory')
|
|
|
- list_filter = ('product_type', 'is_mandatory')
|
|
|
- search_fields = ('name', 'product_type__name')
|
|
|
- inlines = [AttributePossibleValueInline]
|
|
|
+ list_display = ['name', 'product_type', 'is_mandatory', 'possible_values_count']
|
|
|
+ list_filter = ['product_type', 'is_mandatory']
|
|
|
+ search_fields = ['name', 'product_type__name']
|
|
|
+
|
|
|
+ def possible_values_count(self, obj):
|
|
|
+ return obj.possible_values.count()
|
|
|
+ possible_values_count.short_description = 'Possible Values'
|
|
|
|
|
|
|
|
|
@admin.register(AttributePossibleValue)
|
|
|
class AttributePossibleValueAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('attribute', 'value')
|
|
|
- search_fields = ('attribute__name', 'value')
|
|
|
- list_filter = ('attribute__product_type',)
|
|
|
+ list_display = ['value', 'attribute_name', 'product_type']
|
|
|
+ list_filter = ['attribute__product_type', 'attribute__name']
|
|
|
+ search_fields = ['value', 'attribute__name']
|
|
|
+
|
|
|
+ def attribute_name(self, obj):
|
|
|
+ return obj.attribute.name
|
|
|
+ attribute_name.short_description = 'Attribute'
|
|
|
+
|
|
|
+ def product_type(self, obj):
|
|
|
+ return obj.attribute.product_type.name
|
|
|
+ product_type.short_description = 'Product Type'
|