Make boolean values editable in list_display?

2019-04-03 07:18发布

问题:

I'd like a boolean field to be editable in my Django admin's list display. Instead, I have uneditable icons:

My code looks like this:

# Model
class Task(models.Model):
  ...
  is_finished = models.BooleanField() 
# Admin
list_display = (..., 'is_finished')

I haven't included is_finished in the readonly_fields tuple in admin.py, so I'm surprised that it isn't editable by default. What am I doing wrong?

回答1:

ModelAdmin.list_editable is what you need, see its doc here. Below you also have an example:

class TaskAdmin(models.ModelAdmin):
    list_display = (..., 'is_finished')
    list_editable = ('is_finished',) # this MUST only contain fields that also are in "list_display"
    #list_display_links = ('foo', 'bar') # this MUST NOT contain a field in common with "list_editable"