У меня есть Модель данных, и я хочу сохранить значение суммы и значение метода total_amount в методе оплаты, чтобы иметь запись о платеже в базу данных.
** data / models.py **
class Data(models.Model):
""" Model of Data"""
user = models.ForeignKey(User, on_delete=models.CASCADE)
document = models.FileField(upload_to='documents/%Y/%m/%d')
uploaded_at = models.DateTimeField(auto_now_add=True)
amount = models.DecimalField(default=0, max_digits=6, decimal_places=2,
blank=True, null=True)
def total_amount(self):
return Data.objects.filter(user=self.user).aggregate(Sum('amount'))['amount__sum']
payment / models.py
class Payment(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField()
created = models.DateTimeField(auto_now=True)
updated = models.DateTimeField(auto_now=True)
paid = models.BooleanField(default=False)
amount = models.DecimalField(default=0, max_digits=6, decimal_places=2,
blank=True, null=True)
payment / views.py
def payment_create(request):
user = request.user
data = Data.objects.filter(user=user)
if request.method == 'POST':
form = PaymentCreateForm(request.POST)
if form.is_valid():
payment = form.save()
return render(request,
'payments/created.html',
{'payment': payment})
else:
form = PaymentCreateForm()
# template and context
template = 'payments/create.html'
context = {'form': form,
'data': data
}
return render(request,
template,
context
)