1

I have a Django 4.0 model that includes some TextField fields. These are displayed in large text box in the corresponding admin form. How do I reduce the displayed size of a text field in a django admin form, e.g. to just one row?

I have tried setting the widgets dictionary on the form with a Textarea that has the number of rows and cols explictly set, as listed below. However this does not seem to work?

Model

class Product(models.Model):
    """
    Product
    """

    id = models.PositiveBigIntegerField(primary_key=True)
    attributes = models.TextField()

Form and admin

class ProductForm(forms.ModelForm):
    class Meta:
        model = Product
        exclude = ("id",)

    widgets = {
        "attributes": Textarea(attrs={"cols": 30, "rows": 1})
    }

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_per_page = 10
    form = ProductForm
anon_dcs3spp
  • 2,342
  • 2
  • 28
  • 62

1 Answers1

0

You are missing "forms." before calling the widget...

Change:

widgets = {
        "attributes": Textarea(attrs={"cols": 30, "rows": 1})
    }

To:

    widgets = {
        "attributes": forms.Textarea(attrs={"cols": 30, "rows": 1})
    }

(Not tested) Hope this helps :-)