Resize fields in Django Admin

后端 未结 14 2070
一生所求
一生所求 2020-12-22 15:12

Django tends to fill up horizontal space when adding or editing entries on the admin, but, in some cases, is a real waste of space, when, i.e., editing a date field, 8 chara

14条回答
  •  無奈伤痛
    2020-12-22 15:52

    If you are working with a ForeignKey field that involves choices/options/a dropdown menu, you can override formfield_for_foreignkey in the Admin instance:

    class YourNewAdmin(admin.ModelAdmin):
        ...
    
        def formfield_for_foreignkey(self, db_field, request, **kwargs):
            if db_field.name == 'your_fk_field':
                """ For your FK field of choice, override the dropdown style """
                kwargs["widget"] = django.forms.widgets.Select(attrs={
                    'style': 'width: 250px;'
                })
    
            return super().formfield_for_foreignkey(db_field, request, **kwargs)
    

    More information on this pattern here and here.

提交回复
热议问题