14👍
Use a custom form if you don’t want change a model:
from django import forms
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['image'].help_text = 'My help text'
class Meta:
model = MyModel
exclude = ()
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
form = MyForm
# ...
3👍
It took me a while to figure out. If you’ve defined a custom field in your admin.py
only and the field is not in your model. Use a custom ModelForm:
class SomeModelForm(forms.ModelForm):
# You don't need to define a custom form field or setup __init__()
class Meta:
model = SomeModel
help_texts = {'avatar': "User's avatar Image"}
exclude = ()
And in the admin.py:
class SomeModelAdmin(admin.ModelAdmin):
form = SomeModelForm
# ...
- Django error: "'ChoiceField' object has no attribute 'is_hidden'"
- Django not sending error emails – how can I debug?
- Django: values_list() multiple fields concatenated
2👍
If you don’t want to create a custom model form class :
class MyModelAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, change=False, **kwargs):
form = super().get_form(request, obj=obj, change=change, **kwargs)
form.base_fields["image"].help_text = "Some help text..."
return form
- Django migrations conflict multiple leaf nodes in the migration graph
- Python sqlite use in terminal -django
Source:stackexchange.com