How to show all fields of model in admin page?

后端 未结 11 520
無奈伤痛
無奈伤痛 2021-01-30 16:25

here is the models page

In this picture, only the title shows up on here, I used:

 def __unicode__(self):
        return self.title;  

11条回答
  •  梦如初夏
    2021-01-30 16:47

    I'm using Django 3.1.4 and here is my solution.

    I have a model Qualification

    model.py

    from django.db import models
    
    TRUE_FALSE_CHOICES = (
        (1, 'Yes'),
        (0, 'No')
    )
    
    
    class Qualification(models.Model):
        qual_key = models.CharField(unique=True, max_length=20)
        qual_desc = models.CharField(max_length=255)
        is_active = models.IntegerField(choices=TRUE_FALSE_CHOICES)
        created_at = models.DateTimeField()
        created_by = models.CharField(max_length=255)
        updated_at = models.DateTimeField()
        updated_by = models.CharField(max_length=255)
    
        class Meta:
            managed = False
            db_table = 'qualification'
    

    admin.py

    from django.contrib import admin
    from models import Qualification
    
    
    @admin.register(Qualification)
    class QualificationAdmin(admin.ModelAdmin):
        list_display = [field.name for field in Qualification._meta.fields if field.name not in ('id', 'qual_key', 'qual_desc')]
        list_display.insert(0, '__str__')
    

    here i am showing all fields in list_display excluding 'id', 'qual_key', 'qual_desc' and inserting '__str__' at the beginning.

    This answer is helpful when you have large number of modal fields, though i suggest write all fields one by one for better functionality.

提交回复
热议问题