Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

enter image description here

like image 311
koddr Avatar asked Mar 12 '18 14:03

koddr


1 Answers

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
like image 75
Paulo Almeida Avatar answered Nov 08 '22 03:11

Paulo Almeida