Reputation: 91
I registered my model in admin.py, I want to change some model fields name with list of labels. I know how to do it in forms but mentioning labels in Admin model class are not reflecting it on Admin site. I need help how to use list of labels in Admin.py
class eAdmin(admin.ModelAdmin):
list_display = ('name', 'phone_number', 'email_id', 'country', 'state')
list_filter = ('funnel', 'country', 'state')
search_fields = ['email_id']
exclude = ('city',)
class Meta:
labels = labels_list
admin.site.register(EApplications, eAdmin)
I have more than 20 fields I can't do it manually in models.py I want to use labels list
Upvotes: 0
Views: 403
Reputation: 1221
You need to create forms in forms.py
here i give you hit
forms.py
file create in your app
from .models import EApplications
from django import forms
FIELDS_NAME=['name', 'phone_number', 'email_id', 'country', 'state',"etc your field name"]
FIELDS_LABEL=["Enter Name","Enter phone_number","Enter email","Enter country","Enter state","etc your field label"]
label_list=dict(zip(FIELDS_NAME,FIELDS_LABEL)) #dictionary of fields label pare
class EApplicationsForm(forms.ModelForm):
class Meta:
model=EApplications
fields='__all__'
labels=label_list #assign label
admin.py
file in your app need following update
from .forms import EApplicationsForm # import the form in this file
class eAdmin(admin.ModelAdmin):
list_display = ('name', 'phone_number', 'email_id', 'country', 'state')
list_filter = ('funnel', 'country', 'state')
search_fields = ['email_id']
exclude = ('city',)
form=EApplicationsForm #here assign imported form EApplicationsForm
admin.site.register(EApplications, eAdmin)
If you looking more dynamic rather than static, you need to define function which get data from model and zip it with relative fields with labels. And make same thing ahead at all. Just do it at your project and let me know your result...!!
Upvotes: 2
Reputation: 3392
in your models you can add verbose field names
first_name = models.CharField("person's first name", max_length=30)
or you can utilize help_text
help_text="Please use the following format: <em>YYYY-MM-DD</em>."
see https://docs.djangoproject.com/en/3.0/topics/db/models/#verbose-field-names, https://docs.djangoproject.com/en/3.0/ref/models/fields/#help-text
Upvotes: 1