Django Admin Inline返回空的额外实例

Jus*_*cus 5 django django-models django-admin

第一次发布,与Django的Admin TabularInline有点奇怪的问题.似乎无法在任何搜索中找到问题.

当我添加一个值 - 在这种情况下是财务报价 - 并保存条目时,页面将刷新添加实例和另外2个在每个字段中具有空值的条目.

如果我从管理页面标记删除它们,也会发生同样的情况.它删除所有条目,然后再添加3个以上的条目.

Invoice模型(类似的模型)也是如此,但是与预期行为的Purchase模型不同.这让我觉得我在编写模型时已经做了一些奇怪的事情.

附加图像显示结果.

希望有人能看到我出错的地方

谢谢!

models.py

   class Quote(models.Model):
        job = models.ForeignKey(Job, related_name="quotes", on_delete=models.CASCADE)
        number = models.AutoField(primary_key=True)
        currency = models.ForeignKey(Currency, blank=True, null=True)
        amount = models.DecimalField(max_digits=20, decimal_places=2, default="0.00", verbose_name="Amount Invoiced")
        created = models.DateTimeField(auto_now=False, auto_now_add=True)
        created_by = models.ForeignKey(Profile, related_name='quoted', blank=True, null=True, on_delete=models.SET_NULL)
        sent = models.BooleanField(default=False)
        superceded = models.BooleanField(default=False)
        tax = models.DecimalField(max_digits=20,decimal_places=2,default=20.00, verbose_name="Tax Rate")

        def __unicode__(self):
                return  self.created.strftime("%B %d, %Y") + " | "  + u'%s' % (self.currency) + str(self.amount)
        def readable_date(self):
                 return  self.created.strftime("%B %d, %Y")

    class Invoice(models.Model):
        job = models.ForeignKey(Job, related_name="invoices", blank=True, null=True, on_delete=models.SET_NULL)
        number = models.AutoField(primary_key=True)
        currency = models.ForeignKey(Currency, blank=True, null=True)
        amount = models.DecimalField(max_digits=20, decimal_places=2, default="0.00", verbose_name="Amount Invoiced")
        created = models.DateTimeField(auto_now=False, auto_now_add=True)
        created_by = models.ForeignKey('profiles.Profile', related_name='invoiced', blank=True, null=True, on_delete=models.SET_NULL)
        paid = models.BooleanField(default=False)
        sent = models.BooleanField(default=False)
        superceded = models.BooleanField(default=False)
        tax = models.DecimalField(max_digits=20,decimal_places=2,default=20.00, verbose_name="Tax Rate")

        def __unicode__(self):
            return  self.created.strftime("%B %d, %Y") + " | "  + u'%s' % (self.currency) + str(self.amount)
        def readable_date(self):
            return  self.created.strftime("%B %d, %Y")
        def get_day(self):
            return self.created.strftime("%d")
        def get_month(self):
            return self.created.strftime("%b")
Run Code Online (Sandbox Code Playgroud)

admin.py

from finance.models import Purchase, Quote, Invoice
from django.contrib import admin

from .models import Job

class QuoteInline(admin.TabularInline):
    model = Quote
class InvoiceInline(admin.TabularInline):
    model = Invoice
class PurchaseInline(admin.TabularInline):
    model = Purchase
class JobModelAdmin(admin.ModelAdmin):

        list_display = [
        'job_number',
        'brand',
        'job_name',
        'client',
        'account_manager',
        'last_updated_by',
        'updated',
        'status',
        ]

        list_display_links = ['job_name']
        list_filter = ['client']

        inlines = [
            QuoteInline,
            PurchaseInline,
            InvoiceInline
        ]
Run Code Online (Sandbox Code Playgroud)

管理页面中的问题示例

小智 8

在您的内联类集中extra=0.我想你有这个问题,因为你有自己创建的实例中没有任何必填字段的默认值的字段,所以你意外保存它们,并且django没有引发任何错误.