Andrey Orel
Andrey Orel

Reputation: 115

How can create model that couldn't be changed by admin?

I need to create Django model that couldn't by admin, but he should be avialable to see it. It's content will be input from site. How can I do it?

Upvotes: 0

Views: 73

Answers (3)

Jason Yang
Jason Yang

Reputation: 306

Basically if you want to disable the editability of the model you may want to make use of Django Admin's permission framework, like this:

class PersonAdmin
    def has_change_permission(self, request, obj=None):
        # only allows superusers to edit
        return request.user.is_superuser

You may also want to try the readonly_field like:

class PersonAdmin
    readonly_fields = ('name','sex',)

But the problem of doing this is that you will see the save buttons on the editing page despite that nothing is allowed to be changed, which is probably not what you want

Upvotes: 1

Matthew Gaiser
Matthew Gaiser

Reputation: 4783

Designate that it should be read only

In your Model Admin, you can specify that certain fields are not to be changed.

class ProfileAdmin(admin.ModelAdmin):
    readonly_fields = ('source', 'campaign')

Just put that in your admin.py and then when you got to register your site, use this:

admin.site.register(Profile, ProfileAdmin)

instead of what you are probably currently using, which would be

admin.site.register(Profile)

Upvotes: 0

Higor Rossato
Higor Rossato

Reputation: 2046

You can list the fields you want on both fields and readonly_fields.

class AuthorAdmin(admin.ModelAdmin):
    fields = ('name', 'title')
    readonly_fields = ('name', 'title')

Upvotes: 0

Related Questions