【发布时间】:2021-02-07 19:32:50
【问题描述】:
我想开始为我的应用程序使用抽象模型(基于 django-accounting 应用程序)。我应该如何在 views.py 上创建我的字段。我想在创建新字段时我还需要更改我的视图文件...如果它不是抽象模型,我还能像以前一样创建吗?
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
【问题讨论】: