Bart Friederichs
Bart Friederichs

Reputation: 33573

Do not show certain fields in admin page

In Django, you get the model editor for free in the admin/ pages. This all works fine, but I have a few fields in my models that are generated and should never be touched by anybody through a form.

How can I exclude them from these admin/.../change/ forms?

I added exclude to the ModelAdmin:

class exampleAdmin(admin.ModelAdmin):
    exclude = ('field',)

class example(models.Model):
    field = models.CharField(max_length = 100)

Upvotes: 1

Views: 69

Answers (2)

bakkal
bakkal

Reputation: 55448

I have a few fields in my models that are generated and should never be touched by anybody through a form.

You may also use editable=False on your model's field

Field.editable

If False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True.

class Example(models.Model):
    field = models.CharField(max_length=100, editable=False)

Upvotes: 2

4140tm
4140tm

Reputation: 2088

You have to register your exampleAdmin to take effect. In your admin.py add admin.site.register(example, exampleAdmin)

Upvotes: 3

Related Questions