简体   繁体   中英

Django Admin. Disable `list_editable` fields for editing after date?

Django 2.0.3, Python 3.6.1

I have standard Django admin panel. For one of my model I create list_editable with some fields ( provider and provider_price on screenshot). Question is: How to disable those list_editable fields for edit after some date?

For example, if now is March 11, 2018 and older, fields provider and provider_price are disabled and doesn't edit in list view (only in single record change view).

在此输入图像描述

You can use ModelAdmin.get_changelist_form . Something like this:

class TouristVisaInvitationListForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if should_be_disabled(self.instance.date): # whatever condition you want
            self.fields['provider'].disabled = True
            self.fields['provider_price'].disabled = True

class TouristVisaInvitationAdmin(admin.ModelAdmin):
    .
    .
    .
    def get_changelist_form(self, request, **kwargs):
        return TouristVisaInvitationListForm

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