Last active
June 25, 2017 19:21
-
-
Save DerevenetsArtyom/234f83232c09a7cdb5f9778e1712119c to your computer and use it in GitHub Desktop.
[Django Admin Features] # django #admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How add action to admin | |
class PurchaseAdmin(admin.ModelAdmin): | |
actions = [ship] | |
def ship(modeladmin, request, queryset): | |
queryset.update( | |
shipped=True, | |
shipped_at=timezone.now() | |
) | |
ship.short_description = 'Mark purchases as shipped now' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BigOrderFilter(admin.SimpleListFilter): | |
title = 'big order' | |
parameter_name = 'big_order' | |
# What wiil be in dropdown | |
def lookups(self, request, model_admin): | |
return ( | |
('1', 'True'), | |
('0', 'False'), | |
) | |
# Modify qs what will be applied to filter | |
def queryset(self, request, queryset): | |
if self.value() == '1': | |
return queryset.annotate(Count('items')).filter(items__count__gte=2) | |
return queryset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomerAdmin(admin.ModelAdmin): | |
form = forms.CustomerForm | |
list_display = ['admin_name'] | |
save_as = True | |
save_on_top = True | |
search_fields = ['name'] | |
date_hierarchy = 'placed_at' | |
inlines = [PurchaseItemInline] | |
list_editable = ['shipped'] | |
list_filter = ['shipped', 'placed_at', 'shipped_at', BigOrderFilter] | |
ordering = ['placed_at'] | |
search_fields = ['customer__name', 'items__name'] | |
formfield_overrides = { | |
dj_models.TextField: {'widget': widgets.WYSIWYGTextarea} | |
} | |
inlines = [ImageInline] | |
prepopulated_fields = {'slug': ('name',)} | |
radio_fields = {'featured': admin.HORIZONTAL} | |
fieldsets = ( | |
(None, { | |
'fields': ( | |
('customer', 'total', 'shipped'), | |
'discount_code' | |
) | |
}), | |
('Dates', { | |
'classes': ('collapse',), | |
'fields': ('placed_at', 'shipped_at'), | |
}) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def queryset(self, request): | |
qs = super(ProductAdmin, self).queryset(request) | |
qs = qs.prefetch_related("product_item") | |
return qs | |
------------------------------------------------------- | |
def product_item_count(self, obj): | |
return obj.product_item_count | |
def queryset(self, request): | |
qs = super(ProductAdmin, self).queryset(request) | |
return qs.annotate(product_item_count=Count('product_item')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment