2

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

FSp
  • 1,545
  • 17
  • 37
  • Are you using the django dev server or an actual web server and have you collected static files? – Medhat Gayed Mar 27 '14 at 23:06
  • I'm on the dev server, and I've collected the static files ... – FSp Mar 27 '14 at 23:08
  • does the field appear empty in the list of records or the edit page? and what is actually being displayed there? does it show the field name? – Medhat Gayed Mar 27 '14 at 23:10
  • You may have missed to define what fields should be displayed on MyModelAdmin read this https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields – Medhat Gayed Mar 27 '14 at 23:14
  • the field name is shown, but its value appears empty (look here: http://it.tinypic.com/r/2iszdbt/8 all the empty "boxes" should be True/False values from the database ...) now i've found also this Q/A, I wonder if there is a solution to my problem http://stackoverflow.com/questions/14832739/django-admin-how-to-display-widget-on-readonly-field ... running out of ideas ... – FSp Mar 27 '14 at 23:19
  • have you tried the solution here? http://stackoverflow.com/questions/14832739/django-admin-how-to-display-widget-on-readonly-field#14833606 – Medhat Gayed Mar 27 '14 at 23:36
  • Yes, but it was of no help (the proposed solution is about rendering all the fields of a given type with a custom widget ... but not all of my checkboxes must be readonly). Finally I came out with a solution, but thanks for your suggestions (and if you have a different solution, let me know). Thanks again :) – FSp Mar 28 '14 at 00:00

1 Answers1

1

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 (e.g. 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

FSp
  • 1,545
  • 17
  • 37