Home » Show All Fields Of Model In Django Admin

Show All Fields Of Model In Django Admin

Last updated on June 4, 2022 by

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:

show all fields of models in django admin with example

Additionally, read our guide:

  1. Laravel 9 Image Upload Tutorial With Example
  2. Laravel 9 Multiple Database Connections Example
  3. Laravel 9 Automatically Generates Sitemap With Example
  4. Laravel: Change Column Name In Migration
  5. How To Use Where Date Between In Laravel
  6. How To Add Laravel Next Prev Pagination
  7. Laravel Remove Column From Table In Migration
  8. Laravel: Get Month Name From Date
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. How To Update Pivot Table In Laravel
  11. How To Install Vue In Laravel 8 Step By Step
  12. How To Handle Failed Jobs In Laravel
  13. Best Ways To Define Global Variable In Laravel
  14. How To Get Latest Records In Laravel
  15. Laravel Twilio Send SMS Tutorial With Example
  16. How To Pass Laravel URL Parameter
  17. Set Default Value Of Timestamp In Laravel Migration
  18. Laravel 9 File Upload Tutorial With Example
  19. How To Schedule Tasks In Laravel With Example
  20. 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!

 
 

Leave a Comment