boatcoder
boatcoder

Reputation: 18127

How to get Django admin.TabularInline to NOT require some items

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.

Upvotes: 0

Views: 1197

Answers (2)

boatcoder
boatcoder

Reputation: 18127

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.

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 600059

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.

Upvotes: 1

Related Questions