callat-qcd / espressodb

Science database interface using Django as the content manager.
https://espressodb.readthedocs.io
BSD 3-Clause "New" or "Revised" License
8 stars 3 forks source link

TextField max_length has no effect on the row edit window box size for the field #58

Closed kenmcelvain closed 4 years ago

kenmcelvain commented 4 years ago

I set max_length=16, expecting that a single line would be enough. The text field in the row editing page is huge.

ckoerber commented 4 years ago

Hello Ken,

Thanks for the feedback. Without customizations of forms, Django always casts model TextFields to html text areas. The easiest fix on your end would be rendering a TextField as a CharField. If this is not an option, you would have to provide a custom form which casts the specific TextField to a smaller input field.

To do so, you would have to adjust the custom admin.py of the corresponding app. By default EspressoDB registers an admin page for each model.

from espressodb.base.admin import register_admins

register_admins("{project}.{app}")

You have to adjust this to (where {stuff} needs to be replaced)

from django.contrib.admin import site, ModelAdmin
from django.forms import ModelForm, TextInput
# import all models of this app--{MyModel3} needs form adjustments
from {project}.{app}.models import {MyModel1}, {MyModel2}, {MyModel3}

# register models without adjustments (this is what EspressoDB's `register_admins` does)
site.register({MyModel1}, ListViewAdmin)
site.register({MyModel2}, ListViewAdmin)

# register custom new MyModel3 form

## Create new form where `{my_field}` gets a new input type
## See also https://docs.djangoproject.com/en/dev/topics/forms/modelforms/
class New{MyModel3}Form(ModelForm):
    class Meta:
        model = {MyModel3}
        fields = "__all__"
        widgets = {'{my_field}': TextInput}

## Overload EspressoDB's ListViewAdmin to use new form 
class {MyModel3}ListAdmin(ListViewAdmin):
    form = New{MyModel3}Form

## And register new admin with new model
site.register({MyModel3}, {MyModel3}ListAdmin)

Let me know if this helps.

PS: In the future, I would like to include an exclude_models flag for register_admins.

Best,

Chris