Latency to be addressed in django for M2M relationship in ModelAdmin dropdown
I have a group of mailboxes which needs to be populated based on customer login and the domains he owns. Customer:User is 1:1 relationship.
Tried:
views.py:
class MailboxAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
if not self.request.user.is_authenticated:
return Mailbox.objects.none()
qs = Mailbox.objects.all()
# Check if the user is in the 'customers' group
if self.request.user.groups.filter(name='customers').exists():
print('customer login in mailbox autocomplete view.......')
# Filter based on the customer's email
qs = qs.filter(domain__customer__email=self.request.user.email).only('email')
elif self.request.user.groups.filter(name__in=['resellers']).exists():
# Filter based on the reseller's email
qs = qs.filter(domain__customer__reseller__email=self.request.user.email).only('email')
if self.q:
# Further filter based on user input (e.g., email matching)
qs = qs.filter(email__icontains=self.q)
print(qs.values('email'))
return qs
in the apps urls.py: path('mailbox-autocomplete/', views.MailboxAutocomplete.as_view(), name='mailbox-autocomplete'), ]
in models.py:
class GroupMailIdsForm(forms.ModelForm):
class Meta:
model = GroupMailIds
fields = "__all__"
mailboxes = forms.ModelMultipleChoiceField(
queryset=Mailbox.objects.none(),
widget=autocomplete.ModelSelect2Multiple(url='mailmanager:mailbox-autocomplete')
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk: # Check if the instance is being updated
if self.request.user.groups.filter(name='customers').exists():
self.fields['mailboxes'].queryset = Mailbox.objects.filter(domain__customer__email=self.request.user.email)
elif self.request.user.groups.filter(name='resellers').exists():
self.fields['mailboxes'].queryset = Mailbox.objects.filter(domain__customer__reseller__email=self.request.user.email)
in admin.py:
class GroupMailIdsAdmin(ImportExportModelAdmin):
resource_class = GroupMailIdsResource
ordering = ('address',)
filter_horizontal = ('mailboxes',)
and in settings.py:
INSTALLED_APPS = [
'mailmanager.apps.MailmanagerConfig',
'admin_confirm',
'dal',
'dal_select2',
'django.contrib.admin',
'jquery',
]
with other required django apps.
- The autocomplete is not working.
- Django version 4.2 used
- django-autocomplete-light==3.11.0
IS there something I am missing. I am trying to solve the latency issue for loading about 200k mailboxes in the dropdown. Can I get the precise code to resolve this problem. I need to display the selected mailboxes, and also make selection easy. I am not good with js. Also tried filter_horizontal which is a very good display but causes latency due to the huge number of mailboxes
You probably are not using the form in the ModelAdmin
at all, you should inject the .form
[Django-doc] in the admin:
class GroupMailIdsAdmin(ImportExportModelAdmin):
resource_class = GroupMailIdsResource
ordering = ('address',)
filter_horizontal = ('mailboxes',)
form = GroupMailIdsForm