2

Django 1.8. I've got model:

class Location(models.Model):
    address = models.CharField(max_length=65)
    annotation = models.CharField(
        max_length=80, verbose_name='Additional info', blank=True, null=True,
        help_text='e.g. officce 412, 4 floor')

And I have task to expand form input field a bit for Location in admin.py. I made this by:

class LocationAdminForm(forms.ModelForm):
    annotation = forms.CharField(
        widget=forms.widgets.TextInput(attrs={'size': 70}))

and here I lost my verboose_name, blank=True and help_text in admin form. I can fix this problem by:

class LocationAdminForm(forms.ModelForm):
    annotation = forms.CharField(
        required=not Location._meta.get_field('annotation').blank,
        label=Location._meta.get_field('annotation').verbose_name,
        help_text=Location._meta.get_field('annotation').help_text,
        widget=forms.widgets.TextInput(attrs={'size': 70}))

but this variant looks so ugly... Are there any other alternatives here?

steph
  • 103
  • 2
  • 11
valex
  • 5,163
  • 2
  • 33
  • 40
  • You could write a wrapper function that returns the fields that you would want. For example: http://stackoverflow.com/questions/3647805/get-models-fields-in-django – steph Dec 20 '16 at 11:59
  • agree- it is not nice, but I left it to be in my code, it is only a field in a form :) – Ohad the Lad Dec 21 '16 at 08:24
  • @steph wrapper may be an option, thank you. – valex Dec 21 '16 at 09:21
  • 1
    @Ohad i like clear and clean code thats why i asked this question :). And usual Django agree with me, but not in this time :). – valex Dec 21 '16 at 09:23

1 Answers1

1

I highly recommend to modify the css for this admin model instead of changing the form/fields, see ModelAdmin asset definitions:

class LocationAdmin(admin.ModelAdmin):
    class Media:
        css = {
            "all": ("location_admin.css",)
        }

myapp/static/location_admin.css:

#id_annotation { width: 423px }
Udi
  • 29,222
  • 9
  • 96
  • 129