Django Haystack override class's index_queryset in subclass - django

I'm using Django Haystack (with Aldryn Search) to search content on a client site. However, we need to modify the Articles indexed from the Aldryn NewsBlog plugin - Articles assigned to the Intranet Section should not be indexed. So I made a subclass in my plugin to override that like the documentation says to do:
Subclasses can override this method to avoid indexing certain objects.
However, when I try to rebuild the index it says:
aldryn_newsblog.models.Article has more than one 'SearchIndex`` handling it. Please exclude either aldryn_newsblog.search_indexes.ArticleIndex object or search_modifier.search_indexes.BlogHelperIndex object
The documentation is not clear to me what I need to write to have this modify the existing index from the NewsBlog plugin. I don't want to totally exclude it like the error is suggesting, but to subclass it like the documentation says to do.
Here is my search_indexes.py file:
from aldryn_newsblog.search_indexes import ArticleIndex
class BlogHelperIndex(ArticleIndex):
def index_queryset(self):
# make sure only public posts are pulled
return self.get_model().objects.exclude(app_config__app_title='DirectConnection')

I'm a moron. Aldryn Newsblog provides a simple checkbox in the Sections' settings to enable/disable indexing of that section without the need for a code change.

Related

How can I reuse Django admin search feature?

I'm developing an application with Django.I'm using Django admin's search feature like this:
class ProductAdmin(admin.ModelAdmin):
search_fields = ('image_name', 'product_name', )
And it gives a very nice search on these columns. Now I want to use this search in my views and inside my code. I mean I want to reuse this search which Django uses for the admin page in my code.
I've read the code of ModelAdmin class but I couldn't reuse it, because it uses some objects from other layers of Django.
So I couldn't figure out how can I do this.
I found an answer to this. My main concern was being able to use Django admin search because it was completely identical to what I needed and not because of I wanted to adhere to the "write once" philosophy. So I read the code and copied the part which was doing the search thing. I removed some error detection parts and general purpose codes and passed the variables which it needed to work correctly and then it worked.

Hide model from main admin list, but allow creation in inline editor

In my Django app, I have an Attribute model which has a many-to-many relationship to a MeasurementMethod model.
I put an inline for MeasurementMethod in the admin interface for Attribute, but I don't think it is useful to have a separate interface for managing MeasurementMethods at all; there's no reason why a user would say, "Gee, I wonder what Attributes can be measured by water displacement."
However, this left no way to create new MeasurementMethods from the inline editor until I found Anton Belonovich's post, which says that I need to admin.site.register(MeasurementMethod) first. I did that, and sure enough the edit and create buttons appeared.
But now on the admin page, where there's a list of apps and the models that can be managed, there's an entry for MeasurementMethod that I don't want.
Is there a way to get rid of it? Or is there a better way to accomplish this?
The solution is to register the MeasurementMethod class with a custom admin class that overrides has_module_permission:
#admin.register(MeasurementMethod)
class MeasurementMethodAdmin(admin.ModelAdmin):
def has_module_permission(self, request):
return False
Then the class can still be edited inline.
ModelAdmin.has_module_permission(request)
Should return True if displaying the module on the admin index page and accessing the module’s index page is permitted, False otherwise. ... Overriding it does not restrict access to the add, change or delete views ...
You could create a custom admin site docs and then override the index method/view. Make sure you register your models with this new admin site and hook it up in the urls.py file.

How should I extend the Django FeinCMS MediaLibrary?

We would like to extend the MediaLibrary of the Django FeinCMS without editing the module code itself.
We want
a few custom fields
and to use a library for individual cropping that we have already.
How should we put everything together?
The simplest approach would be to create an extensions model with a ForeignKey to the MediaLibrary, and to register a custom Admin site with an inline admin for the extensions model. But maybe there's a cleaner and better way.
As Hedde said, the media file model supports registering extensions the same way as the page module allows, using MediaFile.register_extensions.
The method which is used for generating thumbnails in the media library should always be FEINCMS_MEDIALIBRARY_THUMBNAIL. The default value of this setting is feincms.module.medialibrary.thumbnail.default_admin_thumbnail which is a method that receives a media file object and returns the URL to a thumbnail or None.

How to add report section to the Django admin?

I want to implement a report section in Django admin. This would mean adding a custom section in the admin homepage where instead of a list of models I would see a list of reports. I want to use Django's admin tables with filters, sorting, everything if possible.
What would be the "best" way of achieving this? I realize this is a "big" question so I'm not asking for code snippets necessarily, a summary of needed actions would be just fine :)
P.S. Be report I mean a "made up" model by custom queries (queryset or how it's called).
P.S.2 Maybe this question should be something like: How to use Django admin tables functionality in own admin view?
P.S.3 Or maybe there is a way of providing to the existing admin interface my own data. This way I don't have to do anything else. I just want to say instead of a model take this data and display it in a nice table which I can sort, filter etc etc.
So you are attempting to add in new pages into the django admin.
This section explains to you exactly how you can do so - https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-views-to-admin-sites
The basic idea is to add in new urls that you want in your urls.py as if you are adding urls for your "front end" pages. The key difference is that these new urls you are adding should start with ^admin/ and would look something like ^admin/my_special_link_in_admin and this url will point to your own custom view function at a location you so prefer.
E.g.
(r'^admin/my_special_link_in_admin/$', 'my_custom_admin_app.views.special_admin_page'),
So this is the way for complete customization. There's a very good tutorial which I refer to here - http://brandonkonkle.com/blog/2010/oct/4/django-admin-customization-examples/
In addition, if you don't want to do too much work, consider using Django Admin Plus - https://github.com/jsocol/django-adminplus
Or a django-admin-views - https://github.com/frankwiles/django-admin-views

Use Django admin modules inside own forms

In the Django admin i have a customized changelist with added search and filters. I have been looking alot but cannot seem to find a way to use the whole "changelist module" outside of admin. So i can embed it in one of my own pages.
I do not need any of the authentication or anything like that. I just want to show a table (for a content management backend) that has the nice search, sort and filter capabilities.
Is there perhaps any documentation about doing this?
Of course you can use the ChangeList class for your own projects. I cannot give you a full documentation on doing so here, but some points to start with.
Have a look here to see how the
ChangeList has to be initialized in
your view. (The ChangeList class
lives at
django.contrib.admin.views.main, so
import it from there!)
Look at the admin templates to see how the
corresponding template tags are used.
(also this template)
Maybe you will also find the django.contrib.databrowse-application helpful!