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)
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.
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.
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.
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.
There are several solutions that come to mind:
define what queryset to use for change list with ModelAdmin.queryset().
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With