(抱歉我的英文不好) 我需要向对象字段添加一个值,我将其从formset中排除。我想在视图中自动分配这个。 (我无法修改模型以添加def保存方法并使其成为第三方应用程序模型)
这是模型
class Tax(models.Model):
"""A tax (type+amount) for a specific Receipt."""
tax_type = models.ForeignKey(
TaxType,
verbose_name=_('tax type'),
on_delete=models.PROTECT,
)
description = models.CharField(
_('description'),
max_length=80,
)
base_amount = models.DecimalField(
_('base amount'),
max_digits=15,
decimal_places=2,
)
aliquot = models.DecimalField(
_('aliquot'),
max_digits=5,
decimal_places=2,
)
amount = models.DecimalField(
_('amount'),
max_digits=15,
decimal_places=2,
)
receipt = models.ForeignKey(
Receipt,
related_name='taxes',
on_delete=models.PROTECT,
)
def compute_amount(self):
"""Auto-assign and return the total amount for this tax."""
self.amount = self.base_amount * self.aliquot / 100
return self.amount
class Meta:
verbose_name = _('tax')
verbose_name_plural = _('taxes')
这是表格和表格集
class TaxForm(forms.ModelForm):
class Meta:
model = Tax
fields = [
'tax_type',
'description',
'base_amount',
'aliquot',
]
ReceiptTaxFormset = inlineformset_factory(
Receipt,
Tax,
form=TaxForm,
extra=0,
can_delete=False,
)
这是我处理formset的视图的一部分
if form.is_valid() and entryFormset.is_valid() and taxFormset.is_valid():
receipt = form.save(commit=False)
# Tomamos el punto de venta de la sucursal y lo asignamos
pos = request.user.userprofile.branch_office.point_of_sales
receipt.point_of_sales = pos
receipt.document_number = client.dni_cuit
# Controlamos si el dni o cuit tiene 11 caracteres
# Si los tiene asigna CUIT al típo de documento
if len(client.dni_cuit) == 11:
document_type = DocumentType.objects.get(id=1)
receipt.document_type = document_type
else:
document_type = DocumentType.objects.get(id=10)
receipt.document_type = document_type
# Tomamos los valores de las lineas del comprobante
# y generamos los totales para asentar en el comprobante
total_amount = 0
for f in entryFormset:
cd = f.cleaned_data
qty = cd.get('quantity')
price = cd.get('unit_price')
vat = cd.get('vat')
subtotal = qty * price
total_amount = total_amount + subtotal
for t in taxFormset:
cd = t.cleaned_data
ba = cd.get('base_amount')
al = cd.get('aliquot')
ta = ba * al / 100
total_amount = total_amount + ta
# Asignamos el monto total a total_ammount
# y a net_taxed para factura tipo C ya que es igual
receipt.total_amount = total_amount
receipt.net_taxed = total_amount
# Asignamos 0 para Factura tipo C a los campos no necesarios
receipt.net_untaxed = 0
receipt.exempt_amount = 0
# Guardamos el comprobante y las lineas del mismo
receipt.save()
entryFormset.save()
taxFormset.save()
我需要做的是在taxFormset中,对于我从表单中获得的每个税,将金额分配给对象ta = ba * al / 100
谢谢!
答案 0 :(得分:0)
inlineformset_factory位于使用实例
的modelformset_factory之上我很想看看它给你带来的错误,或者它是不是在保存,但也许你会在这样的事情上失踪。
receipt.instance.total_amount = total_amount
receipt.instance.net_taxed = total_amount
# Asignamos 0 para Factura tipo C a los campos no necesarios
receipt.instance.net_untaxed = 0
receipt.instance.exempt_amount = 0
# Guardamos el comprobante y las lineas del mismo
receipt.save()
entryFormset.save()
taxFormset.save()