How to show all fields of model in admin page?

后端 未结 11 511
無奈伤痛
無奈伤痛 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:42

    Every solution found here raises an error like this

    The value of 'list_display[n]' must not be a ManyToManyField.

    If the model contains a Many to Many field.

    A possible solution that worked for me is:

    list_display = [field.name for field in MyModel._meta.get_fields() if not x.many_to_many]

    0 讨论(0)
  • 2021-01-30 16:43

    I like this answer and thought I'd post the complete admin.py code (in this case, I wanted all the User model fields to appear in admin)

    from django.contrib import admin
    from django.contrib.auth.models import User
    from django.db.models import ManyToOneRel, ForeignKey, OneToOneField
    
    
    MySpecialAdmin = lambda model: type('SubClass'+model.__name__, (admin.ModelAdmin,), {
        'list_display': [x.name for x in model._meta.fields],
        'list_select_related': [x.name for x in model._meta.fields if isinstance(x, (ManyToOneRel, ForeignKey, OneToOneField,))]
    })
    
    admin.site.unregister(User)
    admin.site.register(User, MySpecialAdmin(User))
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-30 16:49

    By default, the admin layout only shows what is returned from the object's unicode function. To display something else you need to create a custom admin form in app_dir/admin.py.

    See here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

    You need to add an admin form, and setting the list_display field.

    In your specific example (admin.py):

    class BookAdmin(admin.ModelAdmin):
        list_display = ('title', 'author', 'price')
    admin.site.register(Book, BookAdmin)
    
    0 讨论(0)
  • 2021-01-30 16:49

    Here is my approach, will work with any model class:

    MySpecialAdmin = lambda model: type('SubClass'+model.__name__, (admin.ModelAdmin,), {
        'list_display': [x.name for x in model._meta.fields],
        'list_select_related': [x.name for x in model._meta.fields if isinstance(x, (ManyToOneRel, ForeignKey, OneToOneField,))]
    })
    

    This will do two things:

    1. Add all fields to model admin
    2. Makes sure that there is only a single database call for each related object (instead of one per instance)

    Then to register you model:

    admin.site.register(MyModel, MySpecialAdmin(MyModel))
    

    Note: if you are using a different default model admin, replace 'admin.ModelAdmin' with your admin base class

    0 讨论(0)
  • 2021-01-30 16:50

    Show all fields:

    list_display = [field.attname for field in BookModel._meta.fields]
    
    0 讨论(0)
提交回复
热议问题