Resize fields in Django Admin

后端 未结 14 2108
一生所求
一生所求 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:49

    The best way I found is something like this:

    class NotificationForm(forms.ModelForm):
        def __init__(self, *args, **kwargs): 
            super(NotificationForm, self).__init__(*args, **kwargs)
            self.fields['content'].widget.attrs['cols'] = 80
            self.fields['content'].widget.attrs['rows'] = 15
            self.fields['title'].widget.attrs['size'] = 50
        class Meta:
            model = Notification
    

    Its much better for ModelForm than overriding fields with different widgets, as it preserves name and help_text attributes and also default values of model fields, so you don't have to copy them to your form.

提交回复
热议问题