Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Manager to filter objects on site but not in admin?

I followed this example and it works great but I'm wondering if I can put in an exception so that when I am in the admin all objects show up (active and inactive). This might be simple but I can't find how to do it in the docs.

Here's what my manager looks like now:

class ShareManager(models.Manager):
    def get_query_set(self):
        return super(ShareManager, self).get_query_set().filter(active=True)
like image 478
knuckfubuck Avatar asked Aug 16 '10 00:08

knuckfubuck


People also ask

How do I add filters to a custom search vertical?

Filters can be added to custom search verticals at the organization and site level. Refinable managed properties are used to configure filters in the vertical administration wizard. Then a custom filter can be created inside a vertical based on a connection property.

How to add a filter to a field in Django?

So, our models look something like this: For the fields that exist in the database it is very simple to add them as a filter option just adding name of the field to list_filter will add a filter of that field on the admin side automatically. This is how Django treats the default field types.

Where can I find filters in SharePoint?

These filters are available in SharePoint Home, Office.com, SharePoint Sites, and Work vertical in Bing. Filters can be added to custom search verticals at the organization and site level. Refinable managed properties are used to configure filters in the vertical administration wizard.

How do I create a search filter on my website?

Select Site information, and then select View all site settings . Look for the Microsoft Search section, and then select Configure search settings. In the navigation pane, go to Custom experience under Microsoft Search and then select Verticals. Select your preferred vertical to create the filter and click Edit.


1 Answers

There are several solutions that come to mind:

  1. define what queryset to use for change list with ModelAdmin.queryset().

  2. install 2 managers on your model, the first one that admin finds will be used as the default one AFAIK.

    class SomeThing(models.Model):
       objects = models.Manager()
       shares = ShareManager()
    
  3. add new method on your custom manager that returns only active stuff and leave get_query_set as it is by default.

    class ShareManager(models.Manager):
        def get_active_items(self):
            return self.get_query_set().filter(active=True)
    

Follow-up

I think the most suitable solution in your case would be combining #1 and variation of #2.

Set your custom manager as objects so everyone can access it (I think this should work for your reusability problem) and also install default manager on your model and use it in ModelAdmin.queryset().

    class SomeThing(models.Model):
       objects = ShareManager()
       admin_objects = models.Manager()

I should have included ModelAdmin.queryset() method example too, so here it is.

    def queryset(self, request):
        qs = self.model.admin_objects.get_query_set()
        # TODO: this should be handled by some parameter to the ChangeList.
        # otherwise we might try to *None, which is bad ;)
        ordering = self.ordering or () 
        if ordering:
            qs = qs.order_by(*ordering)
        return qs

Note the line qs = self.model.admin_objects.get_query_set() is working with admin_objects which is the instance of plain manager and includes unpublished items.

The rest of this implementation of queryset method is default Django's implementation which usually calls qs = self.model._default_manager.get_query_set().

I hope this clears things up a bit.

like image 154
Davor Lucic Avatar answered Oct 09 '22 12:10

Davor Lucic