简体   繁体   中英

django modeladmin, readonly_fields and boolean fields

I've a simple model with a boolean field in it, and the related admin view:

# 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 boolean field appears always empty, independently from the actual value of the field itself.

I didn't find any solution to this problem, does it happen only to me?

I don't know if it may be relevant, but I'm using grappelli == 2.4.5

Thanks

Ok,

after some searching I've found a solution (perfectible, but a good starting point). I've simply overridden the get_form(...) model in my concretization of 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

I renamed the list of my readonly fields to fake_readonly_fields , in order not to mess with Django readonly_fields . This works for textboxes, checkboxes and selects (I guess also for radio buttons, but I didn't verify it...). Now I'm looking for a solution for upload file inputs ...

Btw I don't know if this solution can cause "security" problems (eg some crafted message to the server can overcome my html-disabled fields, and pass new data to overwrite old values ...) but that's a different (still relevant) topic

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