How to show all fields of model in admin page?

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

提交回复
热议问题