Below is my admin view:
@admin.register(AuditStashAwsMasterPolicies)
class AuditPoliciesAdmin(reversion.VersionAdmin):
exclude = ['source_policy_path', 'source_state', 'target_state']
readonly_fields = ['comparison_date', 'source', 'source_policy_name', 'target', 'target_policy_name',
'target_policy_path', 'policy_difference']
def policy_difference(self, instance):
return drift.compare_two_policies(instance.source, instance.source_policy_name, instance.source_policy_path,
instance.target, instance.target_policy_name, instance.target_policy_path)
What I want to do is add some help text to my 'policy_difference' read only field. From the help documentation, I am only able to do this by modifying the model and creating a read-only field there with help text.
The thing is I don't store any values in the 'policy_difference' field I just generate it on the fly and would like to avoid storing it in the model.
Is there any way to add text to the 'policy_difference' read-only field without changing the model AuditStashAwsMasterPolicies?
Django admin by default shows all fields as editable and this fields option will display its data as-is and non-editable. we use readonly_fields is used without defining explicit ordering through ModelAdmin or ModelAdmin.fieldsets they will be added. Now we make the Question text fields read-only.
Putting { { form.as_p }} (or just { { form }}) in your template should display the help_text without additional code, provided that you have form in your context or if you are using individual fields you can use { { form.field.help_text }} to access the help text of particular field. If True, Django will store empty values as NULL in the database.
Every field comes in with built-in validations from Django validators. One can also add more built-in field validations for applying or removing certain constraints on a particular field. help_text attribute is used to display the “help” text along with the field in form in admin interface or ModelForm.
Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. A human-readable name for the field. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces.
You can achieve it by overriding the get_form
method like so:
def get_form(self, request, obj=None, **kwargs):
help_texts = {'policy_difference': 'Help text explaining policy difference'}
kwargs.update({'help_texts': help_texts})
return super(AuditPoliciesAdmin, self).get_form(request, obj, **kwargs)
The help_texts
keyword gets eventually passed to the modelform_factory
method and rendered as the standard help text from a model in the Django admin.
In case you're using an InlineModelAdmin
, you need to override get_formset
in the same manner.
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