Created
July 8, 2019 18:02
-
-
Save rg3915/d4a1156bbbf4740a7ed2a5f161873686 to your computer and use it in GitHub Desktop.
Django Custom Admin short_description
This file contains hidden or 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
| from django.contrib import admin | |
| from .models import Company | |
| @admin.register(Company) | |
| class CompanyAdmin(admin.ModelAdmin): | |
| list_display = ('cnpj', 'name', 'custom_groups') | |
| list_filter = ('groups',) | |
| search_fields = ('cnpj', 'name') | |
| def custom_groups(self, obj): | |
| if obj.groups: | |
| """ | |
| Get group, separate by comma, | |
| and display empty string if user has no group. | |
| """ | |
| if obj.groups.count(): | |
| groups_list = [group.name for group in obj.groups.all()] | |
| return ', '.join(groups_list) | |
| return '' | |
| custom_groups.short_description = 'grupos' | |
| def first_name(self, obj): | |
| if obj.user.first_name: | |
| return obj.user.first_name | |
| first_name.admin_order_field = 'first_name' | |
| first_name.short_description = 'Nome' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment