Opened 6 years ago

Last modified 6 years ago

#28879 closed Uncategorized

Widget NumberInput becomes text if you change size — at Version 3

Reported by: Chris Davies-Barnard Owned by: nobody
Component: Uncategorized Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

I'm adjusting the width of some form fields because small integers such as qty and price don't need to be 20 long and the following code results in them becoming type = 'text' regardless.

    models.DecimalField: {'widget': TextInput(attrs={'size':'7'})},
    models.IntegerField: {'widget': NumberInput(attrs={'size':'3'})},

breaking other aspects. Sorry don't know if I need to add more detail etc...

Change History (3)

comment:1 by Tim Graham, 6 years ago

Description: modified (diff)
Resolution: needsinfo
Status: newclosed

I guess you're putting those lines in a ModelAdmin.formfield_overrides? I tried this and couldn't reproduce the problem. Please reopen if you can provide a sample project or a test case that demonstrates the problem.

comment:2 by Chris Davies-Barnard, 6 years ago

Resolution: needsinfo
Status: closednew

Hi Tim,

Yes, more information below. My javascript call that relied on type=number worked using the model as described but once I included the formfield_overrides it stopped because the inputs had become type=text.

#Invoice Item Class
class MainInvoiceItem(models.Model):
    id = models.AutoField(primary_key=True) # AutoField?
    invoice = models.ForeignKey(MainInvoice, on_delete=models.CASCADE)
    order = models.IntegerField('Order')
    description = models.TextField('Description', max_length=1000)
    qty = models.IntegerField('Qty')
    price = models.DecimalField('Price', max_digits=7, decimal_places=2)
    discount = models.DecimalField('Discount/Mark Up', max_digits=5, decimal_places=2,default=0, )
    vat = models.DecimalField('VAT', max_digits=4, decimal_places=2,default=0, )
    total = models.DecimalField('Total', max_digits=7, decimal_places=2)
    created = models.DateTimeField('Created Date',auto_now_add=True)
    modified = models.DateTimeField('Modified Date',auto_now=True)

    class Meta:
        db_table = 'main_invoice_items'
        verbose_name = 'Item'
        verbose_name_plural = 'Items'
        ordering = ['order']

Presented as inline items in the MainInvoice admin view

##### Invoice Admin #####
class InlineInvoiceItems(admin.TabularInline):
        #form = IndicatorInlineForm
        model = MainInvoiceItem
        extra = 1
        formfield_overrides = {
                models.DecimalField: {'widget': TextInput(attrs={'size':'7'})},
                models.IntegerField: {'widget': TextInput(attrs={'size':'3'})},
                models.TextField: {'widget': Textarea(attrs={'rows':2, 'cols':90})},
        }

This works - but you will note that the html now shows decimal or integer fields created as type=number become type=text. This doesn't cause a major problem unless you are trying to grab type=number with javascript and want to avoid grabbing 'text' fields at the same time.

<td class="field-qty">
  <input type="text" name="maininvoiceitem_set-0-qty" value="1" id="id_maininvoiceitem_set-0-qty" size="3">
<td>

Hope this explains it better.

Chris

comment:3 by Tim Graham, 6 years ago

Description: modified (diff)

Your steps to reproduce use TextInput not NumberInput.

Note: See TracTickets for help on using tickets.
Back to Top