Skip to content

Commit

Permalink
Managing Images in admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmalshahabudeen committed Apr 30, 2023
1 parent 10e5fa3 commit 8c94fcf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions core/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib import admin
from django.contrib.contenttypes.admin import GenericTabularInline
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from store.admin import ProductAdmin
from store.admin import ProductAdmin, ProductImageInline
from store.models import Product
from core.models import User
from tags.models import TaggedItem
Expand All @@ -24,7 +24,7 @@ class TagInline(GenericTabularInline):
autocomplete_fields = ['tag']

class CustomProductAdmin(ProductAdmin):
inlines = [TagInline]
inlines = [TagInline, ProductImageInline]


admin.site.unregister(Product)
Expand Down
Binary file added media/store/images/Ajmal_bg_Pj7YQjm.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 13 additions & 1 deletion store/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ def get_queryset(self, request):
products_count = Count('products')
)


class ProductImageInline(admin.TabularInline):
model = models.ProductImage
readonly_fields = ['thumbnail']

def thumbnail(self, instance):
if instance.image.name != '':
return format_html(f'<img src="{instance.image.url}" class="thumbnail"/>')
return ''

@admin.register(models.Product)
class ProductAdmin(admin.ModelAdmin):
Expand All @@ -52,6 +59,7 @@ class ProductAdmin(admin.ModelAdmin):
prepopulated_fields = {
'slug': ['title']
}
inlines = [ProductImageInline]
list_display = ['title', 'unit_price', 'inventory_status', 'collection_title']
list_editable = ['unit_price']
list_per_page = 10
Expand All @@ -74,6 +82,10 @@ def clear_inventory(self, request, queryset):
f'{updated_count} products were successfully updated',
messages.SUCCESS
)
class Media:
css = {
'all': ['styles.css']
}

@admin.register(models.Customer)
class CustomerAdmin(admin.ModelAdmin):
Expand Down
5 changes: 5 additions & 0 deletions store/static/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.thumbnail {
width: 100px;
height: 100px;
object-fit: cover;
}

0 comments on commit 8c94fcf

Please sign in to comment.