Skip to content

Instantly share code, notes, and snippets.

@rg3915
Created July 8, 2019 18:02
Show Gist options
  • Select an option

  • Save rg3915/d4a1156bbbf4740a7ed2a5f161873686 to your computer and use it in GitHub Desktop.

Select an option

Save rg3915/d4a1156bbbf4740a7ed2a5f161873686 to your computer and use it in GitHub Desktop.
Django Custom Admin short_description
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