Skip to content

Instantly share code, notes, and snippets.

@UnknownPlatypus
Last active November 9, 2021 10:15
Show Gist options
  • Save UnknownPlatypus/76bbd42a57fa299cbfe44547b905242d to your computer and use it in GitHub Desktop.
Save UnknownPlatypus/76bbd42a57fa299cbfe44547b905242d to your computer and use it in GitHub Desktop.
from django.db import models
from django.forms import Textarea
class HowToItemInline(admin.StackedInline):
# All fields of a particular type
formfield_overrides = {
models.TextField: {"widget": Textarea(attrs={"rows": 1, "cols": 80})},
}
# Specific fields
wide_charfields = ["field1", "field2"]
def get_formset(self, request, obj=None, **kwargs):
formset = super().get_formset(request, obj, **kwargs)
for field in self.wide_charfields:
formset.form.base_fields[field].widget = Textarea(attrs={"rows": 2, "cols": 80})
return formset
class HowToAdmin(admin.ModelAdmin):
# All fields of a particular type
formfield_overrides = {
models.TextField: {"widget": Textarea(attrs={"rows": 1, "cols": 80})},
}
# Specific fields
wide_charfields = ["field1", "field2"]
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj, **kwargs)
for field in self.wide_charfields:
form.base_fields[field].widget = Textarea(attrs={"rows": 1, "cols": 80})
return form
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment