python - Django:如何创建一个抽象字段?
问题描述
我想开始为我的应用程序使用抽象模型(基于 django-accounting 应用程序)。我应该如何在 views.py 上创建我的字段。我想在创建新字段时我还需要更改我的视图文件...如果它不是抽象模型,我还能像以前一样创建吗?
视图.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})
模型.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
解决方案
如果您使用基于类的视图 (CBV),则可以使用纯 Python 中的继承。
请注意,您尚未在 AbstractSale 类中定义 Meta 类(并且使用 abstract=True)。如果不这样做,您最终会在数据库中得到一个额外的表。有关更多信息,请参见此处。
推荐阅读
- python - Pyinstaller 说该文件已经存在,但它不存在
- python - 如何在python中修复“ValueError:无法将字符串转换为浮点数:'0180.5052.15'”
- javascript - 如何为依赖于其他返回的函数编写测试?
- deep-learning - cloud_ml_engine scaleType 实现
- linux - 使用 sed 替换“\”使用 linux shell 脚本
- javascript - Express 和 React Routes 如何处理来自浏览器的初始 GET 请求?
- ssl-certificate - Microsoft Exchange 2016 应用 SSL 证书
- javascript - 使动画在过渡后运行
- sql - 何时在 SQL Server 数据仓库中使用用户定义函数
- bigcommerce - 我在哪里可以通过 BigCommerce API 找到 tax_label 值?