Created
May 1, 2020 08:34
-
-
Save hiimdoublej/8b1cce8acf414575703e9529072ca6ca to your computer and use it in GitHub Desktop.
owner admin optimized
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
@admin.register(Owner) | |
class OwnerAdmin(admin.ModelAdmin): | |
list_display = ('name', 'owned_items') | |
def get_queryset(self, request): | |
qs = super().get_queryset(request) | |
qs = qs.prefetch_related('shop_set__item_set') | |
return qs | |
def owned_items(self, obj): | |
items = [] | |
for shop in obj.shop_set.all(): | |
items.extend(shop.item_set.all()) | |
if len(items) > 0: | |
return ','.join(x.name for x in items) | |
return '-' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment