使用Django的投资金额和存款金额的总和如何
我试图使用低于帐户余额的代码线从我的投资中撤回一些金额,而是在提款后没有更新。
investment.basic_investment_return -= investment.basic_withdraw_amount
模型
from django.db import models
class Investment(models.Model):
basic_deposit_amount = models.IntegerField(default=0, null=True)
basic_interest = models.IntegerField(default=0, null=True)
basic_investment_return = models.IntegerField(default=0, null=True)
basic_withdraw_amount = models.IntegerField(default=0, null=True, blank=True)
basic_balance = models.IntegerField(default=0, null=True, blank=True)
investment_id = models.CharField(max_length=10, null=True, blank=True)
is_active = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now=True, null=True)
def save(self, *args, **kwargs):
self.basic_interest = self.basic_deposit_amount * 365 * 0.02/2
self.basic_investment_return = self.basic_deposit_amount + self.basic_interest
super(Investment, self).save(*args, **kwargs)
表格
from django import forms
from .models import Investment
class BasicInvestmentForm(forms.ModelForm):
class Meta:
model = Investment
fields = ['basic_deposit_amount']
class BasicWithdrawalForm(forms.ModelForm):
class Meta:
model = Investment
fields = ['basic_withdraw_amount']
视图
from django.shortcuts import get_object_or_404, redirect, render
from django.db.models import Sum, F
from django.contrib import messages
from .forms import BasicInvestmentForm, BasicWithdrawalForm,
from .models import Investment,
def create_investment_view(request):
if request.method == 'POST':
basic_investment_form = BasicInvestmentForm(request.POST)
if basic_investment_form.is_valid():
investment = basic_investment_form.save(commit=False)
investment.basic_investment_return += investment.basic_deposit_amount
print(investment.basic_investment_return)
investment.is_active = True
investment.save()
messages.success(request, 'your basic investment of {} is successfull '.format(investment.basic_deposit_amount))
else:
messages.success(request, 'your investment is not successfull! Try again.')
else:
basic_investment_form = BasicInvestmentForm()
context = {'basic_investment_form': basic_investment_form}
return render(request, 'create-basic-investment.html', context)
def create_withdrawal_view(request):
if request.method == 'POST':
basic_withdraw_form = BasicWithdrawalForm(request.POST)
if basic_withdraw_form.is_valid():
investment = basic_withdraw_form.save(commit=False)
investment.basic_investment_return -= investment.basic_withdraw_amount
print(investment.basic_investment_return)
investment.save()
messages.success(request, 'your withdrawal of {} is successfull '.format(investment.basic_withdraw_amount))
else:
messages.success(request, 'your withdrawal of {} is unsuccessfull '.format(investment.basic_withdraw_amount))
else:
basic_withdraw_form = BasicWithdrawalForm()
context = {'basic_withdraw_form': basic_withdraw_form}
return render(request, 'create-basic-withdrawal.html', context)
我可以看到控制台的值
print(investment.basic_investment_return)
,但我注意到结果始终是扣除值。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Investment.basic_investment_return始终将要为零,因为您永远不会设置它。
当您调用时,
您会根据表单创建投资实例,该表单具有单个字段,即“ basic_withdraw_amount”。没有提供“ basic_investment_return”',因此它将始终是其默认值,即0:
在“保存函数”中,您从本质上覆盖了您打印的值()
当前在视图中设置的值(打印值)没有被记录,它仅在实例“投资”中存在,直到保存功能覆盖它,无论是在实例和保存到数据库中的版本。
investment.basic_investment_return is always going to be zero to start with, because you never set it.
When you call
you create an investment instance based on the form, which has a single field, 'basic_withdraw_amount'. 'basic_investment_return' isn't provided so it will therefore always be its default value, which is 0:
In the save function, you essentially overwrite the value you print() by setting
Currently the value you set in the view (the printed value) isn't being recorded, it only exists in the instance 'investment' briefly until the save function overrides it, both in the instance and the version saved to the database.
编辑我的答案,因为有以下新信息,并且帖子已更新:
您期望
investment.basic_investment_return
具有等于您投资保存方法。这是行不通的,因为保存方法仅在investment.save()
中执行,这意味着:添加类似的打印行会验证以下内容:
型号
create_investment_view < /strong>
终端将显示以下内容:
如果您打算在保存方法中使用该计算,则
create_investment_view
和create_withdrawal_view
,我建议您像这样因此:模型
上面的观点
只是一个示例,但我希望您能理解这一点。请注意,您还需要在
basic_deposit_amount
中传递create_withdrawal_view
中的值,因此请更新您的 basicwithdrawalform 字段。Editing my answer since there are new information given below and the post has been updated:
You're expecting
investment.basic_investment_return
to have the value equal to the computation in your Investment save method. This will not work because the save method will only execute ininvestment.save()
, which means:Adding print lines like so will verify this:
models
create_investment_view
The terminal will display the following:
If you intend to use the computation in your save method for both
create_investment_view
andcreate_withdrawal_view
, I suggest you move it outside the class like so:models
views
Above is just a sample but I hope you understood the point. Note that you will also need to pass a value for
basic_deposit_amount
in yourcreate_withdrawal_view
, so update your BasicWithdrawalForm fields.我通过从模型类中删除保存方法并在视图中运行所有逻辑来修复它。
I fixed it by deleting the save method from the model class and Running all logic in my views.