Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 595
rgov Avatar asked Mar 15 '18 07:03

rgov


People also ask

How do I hide a field in Django admin panel?

For the admin list-view of the items, it suffices to simply leave out fields not required: e.g. In Django 1.8 exclude = ('fieldname',) does works with admin. ModelAdmin so one does not have to inherit from InlineModelAdmin anymore. Also works in Django 1.6.

How to register model in admin in Django?

from django. contrib import admin # Register your models here. Register the models by copying the following text into the bottom of the file. This code imports the models and then calls admin.

What is Django administration?

django-admin is Django's command-line utility for administrative tasks. This document outlines all it can do. In addition, manage.py is automatically created in each Django project.


1 Answers

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 ...

like image 120
rgov Avatar answered Nov 15 '22 03:11

rgov