Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django editable=False and still show in fieldsets?

I have an image field in my models.py...

 qr_image = models.ImageField(
        upload_to="public/uploads/",
        height_field="qr_image_height",
        width_field="qr_image_width",
        null=True,
        blank=True,
        editable=False
    )

When it's editable=False I get a nasty error when trying show it. I don't want the field editable, however I do want the image to show in admin 'edit' page i.e. fieldsets

I'm new to Django, can someone tell me if this is possible and point me in the right direction here?

thank you.

like image 512
jason Avatar asked Feb 21 '13 16:02

jason


People also ask

How do you make a field not editable in Django?

editable=False will make the field disappear from all forms including admin and ModelForm i.e., it can not be edited using any form.

What is editable false?

editable. If False , the field will not be displayed in the admin or any other ModelForm . They are also skipped during model validation. Default is True .

What is Fieldsets in Django admin?

fieldsets is a list of two-tuples, in which each two-tuple represents a <fieldset> on the admin form page. (A <fieldset> is a “section” of the form.)


1 Answers

If you have a ModelAdmin like this:

class MyModelAdmin(admin.ModelAdmin):

    fieldsets = [
        ('Fieldset', {'fields': ['model_field', 'another_field', 'readonly_field']}),
    ]

where readonly_field has editable=False or if it is a DateTimeField with auto_now_add=True then visiting the admin page for your model will result in a field not found error.

To include a readonly field in your fieldsets, include readonly_fields like this:

class MyModelAdmin(admin.ModelAdmin):

    fieldsets = [
        ('Fieldset', {'fields': ['model_field', 'another_field', 'readonly_field']}),
    ]
    readonly_fields = ('readonly_field',)
like image 67
InnisBrendan Avatar answered Oct 20 '22 06:10

InnisBrendan