Django modeladmin, readonly_fields and logical fields

I have a simple model with a boolean field in it and the admin view associated with it:

# in models.py
class MyModel(models.Model):
    ...
    my_field = models.BooleanField(...)

# in admin.py
class MyModelAdmin(admin.ModelAdmin):

    readonly_fields ("my_field", ...)

My problem is that now my logical field is always empty, regardless of the actual value of the field itself.

I did not find a solution to this problem, does this only happen to me?

I don't know if this might be relevant, but I use grappelli == 2.4.5

thank

0
source share
1 answer

Good,

after some searching, I found a solution (great, but good starting point). I just redefined the model get_form(...)in my instantiation ModelAdmin:

def get_form(self, *args, **kwargs):

    form = super(SupplierAdmin, self).get_form(*args, **kwargs)

    for field_name in self.fake_readonly_fields:
        form.base_fields[field_name].widget.attrs["disabled"] = "disabled"


    return form

readonly fake_readonly_fields, Django readonly_fields. , ( , , ...). ...

Btw , (, - html ...), ( )

0

All Articles