Django Admin Inline 返回空的额外实例

标签 django django-models django-admin

第一次发帖,Django 的 Admin TabularInline 有一个奇怪的问题。似乎无法在任何搜索中找到问题。

当我添加一个值 - 在本例中为财务报价 - 并保存条目时,页面将刷新并添加实例和每个字段中具有空值的额外 2 个条目。

如果我将它们标记为从管理页面删除,也会发生同样的情况。它会删除所有条目,然后再添加 3 个条目来代替之前的条目。

Invoice 模型(这是一个类似的模型)也会发生同样的情况,但不会发生在按预期行为的 Purchase 模型上。这让我觉得我在编写模型时做了一些奇怪的事情。

附上图片以显示结果。

希望有人能看到我哪里出错了

谢谢!

模型.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")

管理文件
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
        ]

Example of issue in admin page

最佳答案

在您的内联类中设置 extra=0 .我猜你有这个问题,因为你有默认值的字段,并且在自动创建的实例中没有任何必填字段,所以你不小心保存了它们,django 没有引发任何错误。

关于Django Admin Inline 返回空的额外实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41130407/

相关文章:

python - 如何在 Django 的 latex 模板中正确设置变量

django - 配置 Django runserver 以在非应用程序目录中提供静态文件?

python - 通过管理员创建新用户时出现 NoReverseMatch 错误

django - 扩展 satchmo 用户配置文件

python - 如何在 ModelAdmin.formfield_for_manytomany() 中使用 Django QuerySet.union()?

django - 在 Django-Admin 中通过 row-id 对显示项进行排序

Django 无法导入模型

python -django : why am I getting this error: AttributeError: 'method_descriptor' object has no attribute 'today' ?

python - 更新多对多关系

python - Django 管理员添加表单 ajax 调用