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.
editable=False will make the field disappear from all forms including admin and ModelForm i.e., it can not be edited using any form.
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 .
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.)
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',)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With