Resize fields in Django Admin

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

    Same answer as msdin but with TextInput instead of TextArea:

    from django.forms import TextInput
    
    class ShortTextField(models.TextField):
        def formfield(self, **kwargs):
             kwargs.update(
                {"widget": TextInput(attrs={'size': 10})}
             )
             return super(ShortTextField, self).formfield(**kwargs)
    
    0 讨论(0)
  • 2020-12-22 15:45

    I had a similar problem with TextField. I'm using Django 1.0.2 and wanted to change the default value for 'rows' in the associated textarea. formfield_overrides doesn't exist in this version. Overriding formfield_for_dbfield worked but I had to do it for each of my ModelAdmin subclasses or it would result in a recursion error. Eventually, I found that adding the code below to models.py works:

    from django.forms import Textarea
    
    class MyTextField(models.TextField):
    #A more reasonably sized textarea                                                                                                            
        def formfield(self, **kwargs):
             kwargs.update(
                {"widget": Textarea(attrs={'rows':2, 'cols':80})}
             )
             return super(MyTextField, self).formfield(**kwargs)
    

    Then use MyTextField instead of TextField when defining your models. I adapted it from this answer to a similar question.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-22 15:52

    To change the width for a specific field.

    Made via ModelAdmin.get_form:

    class YourModelAdmin(admin.ModelAdmin):
        def get_form(self, request, obj=None, **kwargs):
            form = super(YourModelAdmin, self).get_form(request, obj, **kwargs)
            form.base_fields['myfield'].widget.attrs['style'] = 'width: 45em;'
            return form
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-22 15:53

    You can always set your fields sizes in a custom stylesheet and tell Django to use that for your ModelAdmin class:

    class MyModelAdmin(ModelAdmin):
        class Media:
            css = {"all": ("my_stylesheet.css",)}
    
    0 讨论(0)
提交回复
热议问题