Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin Inline returning empty extra instances

First time posting, having a bit of a weird issue with Django's Admin TabularInline. Couldn't seem to find the problem in any searches.

When I add a value - in this case a Financial Quote - and save the entry, the page will refresh having added the instance and an additional 2 entries that have empty values in every field.

The same happens if I flag them for deletion from the admin page. It deletes all entries and then adds 3 more in the place of the previous ones.

The same happens with the Invoice model (which is a similar model) but not with the Purchase models which behaves as expected. This leads me to think i've done something odd when I've written the models.

Image attached to show the result.

Hopefully someone can see where i've gone wrong

Thanks!

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")

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
        ]

Example of issue in admin page

like image 331
Justin Focus Avatar asked Dec 13 '16 20:12

Justin Focus


1 Answers

In your inline classes set extra=0. I guess you have this problem because you have fields with default values and no any required fields in auto-created instances, so you accidentially save them, and django didn't raise any errors.

like image 141
Abdullakh Mursalov Avatar answered Nov 17 '22 01:11

Abdullakh Mursalov