0

I would like to start using an abstract model for my application (based on a django-accounting app). How should I create my field on the views.py. I guess I will also need to change my view file when creating a new field...Can I still create the same way I used to if it was not an abstract model?

views.py

@login_required(login_url="/login/")
def create_bill(request):
    form = BillForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        bill = form.save(commit=False)
        bill.save()
        return render(request, 'accounting/bills/detail.html', {'bill': bill})
    context = {
        "form": form,
    }
    return render(request, 'accounting/bills/create_bill.html', context)

@login_required(login_url="/login/")
def detail_bill(request, bill_id):
    user = request.user
    bill = get_object_or_404(Bill, pk=bill_id)
    return render(request, 'accounting/bills/detail.html', {'bill': bill, 'user': user})

@login_required(login_url="/login/")
def bill_update(request, bill_id):
    bill = get_object_or_404(Bill, pk=bill_id)
    form = BillForm(request.POST or None, instance=bill)
    if form.is_valid():
        form.save()
        return render(request, 'accounting/bills/index_table.html', {'bill': bill})
    else:
        form = BillForm(instance=bill)
    return render(request, 'accounting/bills/edit.html', {'form': form})

models.py

class AbstractSale(CheckingModelMixin, models.Model):
    number = models.IntegerField(default=1,db_index=True)

    # Total price needs to be stored with and wihtout taxes
    # because the tax percentage can vary depending on the associated lines
    total_incl_tax = models.DecimalField("Total (inc. tax)",decimal_places=2,max_digits=12,default=D('0'))
    total_excl_tax = models.DecimalField("Total (excl. tax)",decimal_places=2,max_digits=12,default=D('0'))

    # tracking
    date_issued = models.DateField(default=date.today)
    date_dued = models.DateField("Due date",blank=True, null=True,help_text="The date when the total amount ""should have been collected")
    date_paid = models.DateField(blank=True, null=True)

class AbstractSaleLine(models.Model):
    label = models.CharField(max_length=255)
    description = models.TextField(blank=True, null=True)
    unit_price_excl_tax = models.DecimalField(max_digits=8,decimal_places=2)
    quantity = models.DecimalField(max_digits=8,decimal_places=2,default=1)

    class Meta:
        abstract = True

class Bill(AbstractSale):
    organization = models.ForeignKey('Organization',on_delete=models.CASCADE, related_name="bills", verbose_name="To Organization")
    client = models.ForeignKey('contacts.Client', on_delete=models.CASCADE,verbose_name="From client")
    payments = GenericRelation('Payment')


class BillLine(AbstractSaleLine):
    bill = models.ForeignKey('Bill',related_name="lines",on_delete=models.CASCADE)
    tax_rate = models.ForeignKey('TaxRate',on_delete=models.CASCADE)

    class Meta:
        pass
Steve
  • 31
  • 12

1 Answers1

0

if you use class-based-views (CBV) you can use inheritance as found in pure python.

Note that you have not defined a Meta class (and with abstract=True) within your AbstractSale class. By not doing this, you will end up with an additional table in your database. For more info see here.

andyw
  • 3,505
  • 2
  • 30
  • 44