admin.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from django.contrib import admin
  2. from .models import Product, ProductType, ProductAttribute, AttributePossibleValue
  3. # @admin.register(Product)
  4. # class ProductAdmin(admin.ModelAdmin):
  5. # list_display = ('item_id', 'product_name', 'product_type', 'image_path')
  6. # search_fields = ('item_id', 'product_name', 'product_type')
  7. # list_filter = ('product_type',)
  8. # readonly_fields = ('image_path',)
  9. # ordering = ('product_name',)
  10. class AttributePossibleValueInline(admin.TabularInline):
  11. model = AttributePossibleValue
  12. extra = 1
  13. # @admin.register(ProductAttribute)
  14. # class ProductAttributeAdmin(admin.ModelAdmin):
  15. # list_display = ('name', 'product_type', 'is_mandatory')
  16. # list_filter = ('product_type', 'is_mandatory')
  17. # search_fields = ('name', 'product_type__name')
  18. # inlines = [AttributePossibleValueInline]
  19. # @admin.register(AttributePossibleValue)
  20. # class AttributePossibleValueAdmin(admin.ModelAdmin):
  21. # list_display = ('attribute', 'value')
  22. # search_fields = ('attribute__name', 'value')
  23. # list_filter = ('attribute__product_type',)
  24. # admin.py
  25. from django.contrib import admin
  26. from .models import Product, ProductType, ProductAttribute, AttributePossibleValue, ProductAttributeValue
  27. @admin.register(ProductAttributeValue)
  28. class ProductAttributeValueAdmin(admin.ModelAdmin):
  29. """Admin interface for managing original attribute values."""
  30. list_display = ['item_id_display', 'attribute_name', 'original_value', 'updated_at']
  31. list_filter = ['attribute_name', 'created_at', 'updated_at']
  32. search_fields = ['product__item_id', 'product__product_name', 'attribute_name', 'original_value']
  33. readonly_fields = ['created_at', 'updated_at']
  34. fieldsets = (
  35. ('Product Information', {
  36. 'fields': ('product',)
  37. }),
  38. ('Attribute Details', {
  39. 'fields': ('attribute_name', 'original_value')
  40. }),
  41. ('Timestamps', {
  42. 'fields': ('created_at', 'updated_at'),
  43. 'classes': ('collapse',)
  44. }),
  45. )
  46. def item_id_display(self, obj):
  47. return obj.product.item_id
  48. item_id_display.short_description = 'Item ID'
  49. item_id_display.admin_order_field = 'product__item_id'
  50. def get_queryset(self, request):
  51. """Optimize queries by selecting related product."""
  52. return super().get_queryset(request).select_related('product')
  53. class ProductAttributeValueInline(admin.TabularInline):
  54. """Inline admin for showing attribute values in Product admin."""
  55. model = ProductAttributeValue
  56. extra = 1
  57. fields = ['attribute_name', 'original_value', 'updated_at']
  58. readonly_fields = ['updated_at']
  59. @admin.register(Product)
  60. class ProductAdmin(admin.ModelAdmin):
  61. """Enhanced Product admin with inline attribute values."""
  62. list_display = ['item_id', 'product_name', 'product_type', 'attribute_count']
  63. list_filter = ['product_type']
  64. search_fields = ['item_id', 'product_name', 'product_type']
  65. inlines = [ProductAttributeValueInline]
  66. fieldsets = (
  67. ('Basic Information', {
  68. 'fields': ('item_id', 'product_name', 'product_type')
  69. }),
  70. ('Descriptions', {
  71. 'fields': ('product_short_description', 'product_long_description'),
  72. 'classes': ('collapse',)
  73. }),
  74. ('Media', {
  75. 'fields': ('image_path', 'image')
  76. }),
  77. )
  78. def attribute_count(self, obj):
  79. """Display count of original attribute values."""
  80. return obj.attribute_values.count()
  81. attribute_count.short_description = 'Original Attributes'
  82. def get_queryset(self, request):
  83. """Optimize queries."""
  84. return super().get_queryset(request).prefetch_related('attribute_values')
  85. @admin.register(ProductType)
  86. class ProductTypeAdmin(admin.ModelAdmin):
  87. list_display = ['name', 'attribute_count']
  88. search_fields = ['name']
  89. def attribute_count(self, obj):
  90. return obj.attributes.count()
  91. attribute_count.short_description = 'Attributes'
  92. @admin.register(ProductAttribute)
  93. class ProductAttributeAdmin(admin.ModelAdmin):
  94. list_display = ['name', 'product_type', 'is_mandatory', 'possible_values_count']
  95. list_filter = ['product_type', 'is_mandatory']
  96. search_fields = ['name', 'product_type__name']
  97. def possible_values_count(self, obj):
  98. return obj.possible_values.count()
  99. possible_values_count.short_description = 'Possible Values'
  100. @admin.register(AttributePossibleValue)
  101. class AttributePossibleValueAdmin(admin.ModelAdmin):
  102. list_display = ['value', 'attribute_name', 'product_type']
  103. list_filter = ['attribute__product_type', 'attribute__name']
  104. search_fields = ['value', 'attribute__name']
  105. def attribute_name(self, obj):
  106. return obj.attribute.name
  107. attribute_name.short_description = 'Attribute'
  108. def product_type(self, obj):
  109. return obj.attribute.product_type.name
  110. product_type.short_description = 'Product Type'