form to override autocomplete organization field for Person class in admin

This commit is contained in:
asamihassan 2022-03-27 23:32:26 +05:00
parent d436650d2c
commit e8f8ee8ad3

View file

@ -7,6 +7,21 @@ from django.forms import TextInput
from django.conf import settings
from django.http import HttpResponse
import csv
from django import forms
from django.contrib.admin.widgets import AutocompleteSelect
class autocomplete_fields_form(forms.ModelForm):
"""This form overrides autocomplete
organization field for Person class
"""
class Meta:
widgets = {
'my_field': AutocompleteSelect(
Person._meta.get_field('organization').remote_field,
admin.site,
attrs={'data-dropdown-auto-width': 'true'}
),
}
def download_csv(modeladmin, request, queryset):
name = queryset[0].__class__.__name__
@ -85,13 +100,13 @@ class PersonAdmin(admin.ModelAdmin):
}),
)
list_display = ['id', 'full_name', 'organization']
autocomplete_fields = ['organization']
#autocomplete_fields = ['organization']
form = autocomplete_fields_form # adjust width of autocomplete_fields organization
list_per_page = settings.ADMIN_LIST_PER_PAGE
ordering = ['full_name']
actions = [download_csv]
# list_display_links = ['id']
class ResourceKeywordInline(admin.TabularInline):
autocomplete_fields = ['keyword']