pythontestingdjango-admin-filters

Change django admin.simplelistfilter value for querying during testing


I have a django admin filter which inherits from simplelistfilter. I'm trying to change the value of self.value() programmatically for testing purposes, but can't figure it out. Would appreciate any help.

Code here (note the TODOs): https://github.com/makinacorpus/django-safedelete/pull/175/files

The maintainer of the project has tried to help me, but I can't figure it out based on his answer: https://github.com/makinacorpus/django-safedelete/pull/175


Solution

  • Solved this as follows:

    request = self.request_factory.get('/', {<field>: <value>})
    request.user = self.request.user
    changelist = self.modeladmin.get_changelist_instance(request)
    queryset = changelist.get_queryset(request)
    

    Where <field> is the field you're filtering and <value> is its desired value in the admin filter's lookups.

    The queryset will contain only the filtered objects.