tags:

views:

337

answers:

2
class LineItemInline(admin.TabularInline):
    model = LineItem
    extra = 10

class InvoiceAdmin(admin.ModelAdmin):
    model = Invoice
    inlines = (LineItemInline,)

and

class LineItem(models.Model):
    invoice = models.ForeignKey(Invoice)
    item_product_code = models.CharField(max_length=32)
    item_description = models.CharField(max_length=64)
    item_commodity_code = models.ForeignKey(CommodityCode)
    item_unit_cost = models.IntegerField()
    item_unit_of_measure = models.ForeignKey(UnitOfMeasure, default=0)
    item_quantity = models.IntegerField()
    item_total_cost = models.IntegerField()
    item_vat_amount = models.IntegerField(default=0)
    item_vat_rate = models.IntegerField(default=0)

When I have it setup like this, the admin interface is requiring me to add data to all ten LineItems. The LineItems have required fields, but I expected it to not require whole line items if there was no data entered.

+1  A: 

That's strange, it's supposed not to do that - it shouldn't require any data in a row if you haven't entered anything.

I wonder if the default options are causing it to get confused. Again, Django should cope with this, but try removing those and see what happens.

Also note that this:

item_unit_of_measure = models.ForeignKey(UnitOfMeasure, default=0)

is not valid, since 0 can not be the ID of a UnitOfMeasure object. If you want FKs to not be required, use null=True, blank=True in the field declaration.

Daniel Roseman
A: 

Turns out the problem is default values. The one pointed out above about UnitOfMeasure isn't the actual problem though, any field with a default= causes it to require the rest of the data to be present. This to me seems like a bug since a default value should be subtracted out when determining if there is anything in the record that needs saving, but when I remove all the default values, it works.

In this code, item_unit_of_measure = models.ForeignKey(UnitOfMeasure, default=0) it was a sneaky way of letting the 0th entry in the database be the default value. That doesn't work unfortunately as he pointed out though.

Mark0978
Odd. Are you using an old version of Django? This very bug was fixed about a year ago (http://code.djangoproject.com/ticket/7975), and tests were included in the fix, so I'd be surprised if it regressed.
Carl Meyer