简体   繁体   中英

Django - make list_editable only for some entries

I have a model Person which I want to have it in admin panel.

I created PersonAdmin with the following:

class PersonAdmin(admin.ModelAdmin):
    list_display = ["Name", "LastJob", "CurrentJob", "status"]
    list_editable = ["CurrentJob"]

    class Meta:
        model = Person

admin.site.register(Person, PersonAdmin)

status tells me if the person is dead or alive.

I want if status is dead -> CurrentJob to not be editable anymore.

Do you have any suggestions how can I do this?

Thank you!

The Django model admin provides a special method for this: has_change_permission . In your case it would be something as:

class PersonAdmin(admin.ModelAdmin):
    list_display = ["Name", "LastJob", "CurrentJob", "status"]
    list_editable = ["CurrentJob"]

    class Meta:
        model = Person

    def has_change_permission(request, obj=None):
        if obj:
            return obj.status != 'dead'

admin.site.register(Person, PersonAdmin)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM