user956424
user956424

Reputation: 1609

How to use the django-admin-search with search_FieldNameHere ? I am unable to use the same

The django-admin-search plugin version 0.3.5 has beem installed with django 3.0.5 on linux. How exactly to use the search_field query, I fail to understand. I am not great working with OOPS yet. What I tried:

 def search_mfrom(request, field_value, param_values):
     """
     intercept query filter for description field
     """
     query = Q()
     #print(request, field_value, param_values, 'llllllllllllll')
     query = Q(mfrom='[email protected]')
     print(query, 'qqqqqqqqqqqq')
     return query

The print(query, 'qqqqqqqqqqq') never executes. Am I passing in something wrong? The form field is mfrom.

Secondly, wish to send in login specific records in queryset which this tool is overriding. How to do that? I am able to override the queryset based on login domain part of the request.user, but since this tool is also overriding the queryset object of the modeladmin, I don't know how to go about. The code snippet for the same:

def get_queryset(self, request):
    domain_list = []
    temp = []
    qs = super(LogSearchFormAdmin, self).get_queryset(request)
    if request.user.is_superuser:
        return qs
    else:
        domain_list = Domain.objects.filter(
            customer__in=Customer.objects.filter(
                email=request.user.username)).values_list(
                    'name', flat=True)
        #print(list(domain_list))
        dom_names = list(domain_list)
        #print(dom_names)
        qs = MailLogs.objects.none()
        if dom_names:
            for d in dom_names:
                qs = MailLogs.objects.filter(
                    Q(mfrom__icontains=d)|Q(mto__icontains=d))
                
                # qs |= MailLogs.objects.filter(mfrom__icontains=d)

                # qs |=  MailLogs.objects.filter(mto__icontains=d)

                print(qs.query)
        #print(qs)
        return qs

Upvotes: 0

Views: 114

Answers (1)

I believe it is a mistake in the documentation for this case, I will update it coming soon.

I am not near my development laptop in this week, for this reasson I try to help you without testing code, sorry for this.

Another details is: I create this lib for Django 2, and in this pandemic a lot has happened, international change, visa details, search for house, new job, and more, it's crazy days. And I not have certain if works with Django 3. In next month I check this (I believe I will have solved everything)

But if works, all your need is:

  1. Install a lib (check GitHub page)

  2. Create your search form

from django.forms import Form
from django.forms import CharField

class YourFormSearch(Form):
    mfrom = CharField()  ## this type is just for registry will be ignored because your override this
  1. Override in the admin
class YourAdmin(AdvancedSearchAdmin):
    def search_mfrom(self, field, field_value, form_field, request, param_values):
        """
            intercept mfrom filter and override
        """
        query = Q()
        # your Q logic here
        return query

Upvotes: 1

Related Questions