Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django conditional admin list_editable

Is there anyway to make the list_editable optional on a per object bases? For example the readonly fields attribute has this option, which doesn't affect the changelist_view.

class MyAdmin(admin.ModelAdmin):
    readonly_fields = ('foo',)

    def get_readonly_fields(self, request, obj=None):
        fields = super(MyAdmin, self).get_readonly_fields(request, obj=obj)

        if obj.status == 'CLOSED':
            return fields + ('bar',)
        return fields

The same can be achieved for list_display and some other attributes. It seems there isn't a method 'get_list_editable_fields'.

I want some of the rows to be immutable obviously, but other than raising a vulgar error doesn't seem to work. I didn't find any documentation about the attribute either

Would it somehow be possible to render the widget via a list_display getter?

class MyAdmin(admin.ModelAdmin):
    list_display = ('get_bar',)
    list_editable = ('get_bar',)

    def get_bar(self, obj):
        return widget or str(obj.bar)  # ???
    get_bar.allow_tags = True

update using Alasdair's feedback:

def get_changelist_formset(self, request, **kwargs):
    """
    Returns a FormSet class for use on the changelist page if list_editable
    is used.
    """
    # I run through this code for each row in the changelist, but there's nothing in kwargs, so I don't know how to use the instance as a guide to which fields should be in list_editable?

    defaults = {
        "formfield_callback": partial(self.formfield_for_dbfield, request=request),
    }
    defaults.update(kwargs)
    return modelformset_factory(
        self.model, self.get_changelist_form(request), extra=0,
        fields=self.list_editable, **defaults
    )
like image 805
trb Avatar asked Oct 29 '15 13:10

trb


2 Answers

As you say, there is no get_list_editable method.

Try overriding the get_changelist_formset method. I think you'll need to duplicate the entire method, and change the list of fields passed to modelformset_factory.

like image 93
Alasdair Avatar answered Oct 03 '22 02:10

Alasdair


As said, there is not get_list_editable method in the ModelAdmin class, but it is possible to implement it easily (tested on django==2.1):

class MyAdminClass(admin.ModelAdmin):

    def get_list_editable(self, request):
        """
        get_list_editable method implementation, 
        django ModelAdmin doesn't provide it.
        """
        dynamically_editable_fields = ('name', 'published', )
        return dynamically_editable_fields

    def get_changelist_instance(self, request):
        """
        override admin method and list_editable property value 
        with values returned by our custom method implementation.
        """
        self.list_editable = self.get_list_editable(request)
        return super(MyAdminClass, self).get_changelist_instance(request)
like image 22
Fabio Caccamo Avatar answered Oct 03 '22 03:10

Fabio Caccamo