In this tutorial, we will learn how to show all fields of a model in Django admin step by step with an example. Django by default shows the first field of the model in the admin panel. Let’s show all the fields of the model in Django admin. Let’s see how to do it.
Show All Fields Of Model In Django Admin
The list_display is used to show model fields in the admin table. To show fields you need to pass the names of the fields below:
Example:
list_display = ('first_name', 'last_name')
To display all the fields of your model in the admin:
list_display = [field.name for field in YOURMODEL._meta.get_fields()]
list_display = [field.name for field in Setting._meta.get_fields()]
For this article, we will display all the fields of the model. Let’s see a simple example of settings
menu in Django admin.
models.py
In models.py
we will add Setting
model with Key
& Value
two columns. Let’s open it and add the below code.
from django.db import models
from django.contrib import admin
# Create your models here.
class Home(models.Model):
image = models.ImageField(upload_to='uploads')
class Setting(models.Model):
key = models.CharField(max_length=100, verbose_name='Key')
value = models.TextField(verbose_name='Value')
admin.py
By default, Django will show only the Key column in the Django admin panel but we will want to show all the columns. Let’s open the admin.py
and add the below code:
from django.contrib import admin
from .models import Home
from .models import Setting
# Register your models here.
class SettingAdmin(admin.ModelAdmin):
list_display = ('key', 'value')
#list_display = [field.name for field in Setting._meta.get_fields()]
admin.site.register(Home)
admin.site.register(Setting, SettingAdmin)
Output – Show all fields of model in django admin
Hurray! We have completed all steps to show all fields of the model in Django admin with an example. After adding the above code. Let’s visit the Django admin and see the Settings admin menu. It should look like the below:
Additionally, read our guide:
- Laravel 9 Image Upload Tutorial With Example
- Laravel 9 Multiple Database Connections Example
- Laravel 9 Automatically Generates Sitemap With Example
- Laravel: Change Column Name In Migration
- How To Use Where Date Between In Laravel
- How To Add Laravel Next Prev Pagination
- Laravel Remove Column From Table In Migration
- Laravel: Get Month Name From Date
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Update Pivot Table In Laravel
- How To Install Vue In Laravel 8 Step By Step
- How To Handle Failed Jobs In Laravel
- Best Ways To Define Global Variable In Laravel
- How To Get Latest Records In Laravel
- Laravel Twilio Send SMS Tutorial With Example
- How To Pass Laravel URL Parameter
- Set Default Value Of Timestamp In Laravel Migration
- Laravel 9 File Upload Tutorial With Example
- How To Schedule Tasks In Laravel With Example
- Laravel Collection Push() And Put() With Example
That’s it from our end. We hope this article helped you to show all fields of models in Django admin with an example.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!