|
@@ -1,5 +1,8 @@
|
|
|
from django.contrib import admin
|
|
|
+from django.contrib.auth.models import User
|
|
|
from django.contrib.contenttypes.admin import GenericStackedInline
|
|
|
+from django.db.models import Q
|
|
|
+from django.contrib.contenttypes.models import ContentType
|
|
|
|
|
|
from .forms import AdherentForm
|
|
|
from .models import Corporation, Adherent
|
|
@@ -39,6 +42,21 @@ class AdherentAdmin(admin.ModelAdmin):
|
|
|
list_filter = (AdherentTypeFilter,)
|
|
|
fields = ('id',)
|
|
|
readonly_fields = ('id',)
|
|
|
+ search_fields = ('id',)
|
|
|
+
|
|
|
+ def get_search_results(self, request, queryset, search_term):
|
|
|
+ queryset, use_distinct = super().get_search_results(request, queryset, search_term)
|
|
|
+ users = User.objects.filter(
|
|
|
+ Q(username__icontains=search_term)
|
|
|
+ | Q(first_name__icontains=search_term)
|
|
|
+ | Q(last_name__icontains=search_term)
|
|
|
+ )
|
|
|
+ user_type = ContentType.objects.get_for_model(User)
|
|
|
+ queryset |= Adherent.objects.filter(adherent_type=user_type, adherent_id__in=users.values_list('pk'))
|
|
|
+ corporations = Corporation.objects.filter(social_reason__icontains=search_term)
|
|
|
+ corporation_type = ContentType.objects.get_for_model(Corporation)
|
|
|
+ queryset |= Adherent.objects.filter(adherent_type=corporation_type, adherent_id__in=corporations.values_list('pk'))
|
|
|
+ return queryset, use_distinct
|
|
|
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
|
# get_inlines does not exists :-(
|