Home > Software engineering >  Certain models not showing up in django 4 admin despite being registered
Certain models not showing up in django 4 admin despite being registered

Time:04-02

Using django 4.0.3 and the restframework.

I have serval models registered in django admin:

@admin.register(models.Drug)
class DrugAdmin(admin.ModelAdmin):
    list_display = ('apl_label', 'name', 'drug_class', 'atc_code', )
    
@admin.register(models.Organism)
class OrganismAdmin(admin.ModelAdmin):
    list_display = ('apl_label', 'long_name', 'short_name', 'genus', 'flagged', 'notes')

....

For some reason, some o fthe models are not showing up in the admin view. In this example, Organism shows up, but Drug is hidden. I can go to http://localhost:8000/admin/api/drug/ and access this admin page, but it is not listed under http://localhost:8000/admin/. There is no error message either. I did run makemigrations and migrate.

Permissions are only set globally in settings.py:

...
'DEFAULT_AUTHENTICATION_CLASSES': [
    'rest_framework.authentication.SessionAuthentication',
],

It is always the same models not showing up. I tried deleting the database and creating everything from scratch. No luck. Why does Django decide to not show certain models?

enter image description here

CodePudding user response:

If this solves your problem you can use this. Or you can check this to understand is the problem because of your admin.py or not.

This will work properly:

from django.contrib import admin
from .models import (
    Drug,
    Organism,
    )

admin.site.register(Drug)
admin.site.register(Organism)

CodePudding user response:

That should not happen. Your code seems perfectly fine to me. Make sure your top imports contain the following lines too:-

from django.contrib import admin
from . import models

If your models don't have any extra customization like custom fieldsets and/or custom section-titles, you can try using the regular old admin.site.register(models.modelname). Maybe the decorator is malfunctioning for you for some reason, though I have never seen that happen before.

  • Related