Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding a model field in Django admin 1.9

I have registered some models to display in the admin area, but I would like for some fields to be hidden.

As an example, I have a TeachingClasses model with a BooleanField named 'Status' that is set to True or False depending if the class is open or not. But that is set somewhere else in the app. There is no need to have that field displayed in the admin area when someone wants to create a new class to attend.

As such, is there a way to hide that field in the admin area?

I have tried adding this to the app admin.py file but it did nothing

from django.contrib import admin

class MyModelAdmin(admin.ModelAdmin):
    class TeachingClasses:
        exclude = ('Status',)

but it's not working?

Any clue if this is the right way?

My model:

class TeachingClasses(models.Model):
    name = models.Charfield('Class Name',max_lenght=64)
    [...]
    status = models.BooleanField('Status',default=True)
like image 724
Kinwolf Avatar asked Feb 18 '16 20:02

Kinwolf


1 Answers

What you did is not the correct syntax, you need:

class TeachingClassesAdmin(admin.ModelAdmin):
    exclude = ('status',)

admin.site.register(TeachingClasses, TeachingClassesAdmin)

Django doc about how to use exclude.

like image 173
Shang Wang Avatar answered Oct 24 '22 18:10

Shang Wang