gpt4 book ai didi

Django save() 方法不保存

转载 作者:行者123 更新时间:2023-12-04 16:00:03 25 4
gpt4 key购买 nike

当进行新投资时,我正在使用信号来更新配置文件表中的字段。

在 update_users_investments 函数中使用了许多打印语句后,我可以看到正在调用该函数并且它正在执行正确的计算。

但是,它没有保存到数据库中,也没有错误。

你能明白为什么它不保存吗?有没有更好的方法来调试保存方法?我环顾四周,什么也看不见,也不知道为什么不节省是很令人沮丧的。

在我的代码的其他地方,我使用类似的信号来保存到其他看起来工作正常的表。

信号

@receiver(post_save, sender=Investment)
def update_users_investments(sender, instance, created, **kwargs):
user = Profile.objects.get(user=instance.user.user)
total_investments = user.total_invested
investment = instance.transaction_type * instance.price * instance.number_shares
user.total_invested = total_investments + investment
user.save()

要保存到的模型:
class Profile(models.Model):

user = models.OneToOneField(User, on_delete=models.CASCADE)
total_invested = models.DecimalField(max_digits=5, decimal_places=2)
cash_avaliable = models.DecimalField(max_digits=5, decimal_places=2,
null=True)

投资模型(这是信号的来源):
class Investment(models.Model):

"""Model representing each investment made by users"""

user = models.ForeignKey(Profile, on_delete=models.DO_NOTHING)

team_code = models.ForeignKey(Team,
on_delete=models.DO_NOTHING)

transaction_type = models.IntegerField(choices=((-1, "Sell"), (1, "Buy")),
help_text="Show whether transaction\
was a buy or sell",
verbose_name="Transaction Type")

number_shares = models.IntegerField(help_text="Number of shares bought or \
sold in this transaction",
verbose_name="Number of Shares")

price = models.DecimalField(help_text = "Current Price of the team",
verbose_name = "Current Price", decimal_places=2, max_digits=5)

transaction_date = models.DateTimeField(auto_now=True, help_text="Date and \
Time of transaction",
verbose_name="Transaction Date")

transaction_mode = models.IntegerField(choices=((-1, "User Generated"),
(1,
"Automatically Generated")))

编辑

已经解决了这个问题 - 正如所指出的,不需要 user = Profile.objects.get(user=instance.user.user)。
@receiver(post_save, sender=Investment)
def update_users_investments(sender, instance, created, **kwargs):
total_investments = instance.user.total_invested
investment = instance.transaction_type * instance.price * instance.number_shares
instance.user.total_invested = total_investments + investment
instance.user.save()

现在似乎使它工作:-)

最佳答案

我遇到了同样的问题,我所有的调试打印看起来都是正确的,但我无法获得数据集。

根据https://realpython.com/modeling-polymorphism-django-python/ :

“您使用 Django’s built-in validation mechanism 来强制执行数据完整性规则。clean() 仅由 Django 表单自动调用。对于不是由 Django 表单创建的对象,您需要确保明确验证该对象。”

我尝试直接保存到字段并在 View 中使用 save() 以及在模型中使用方法。例如:

def set_date_paid(self,date_str):
self.date_paid = date_str

使用这种方法,我仍然在调用该方法的 View 中执行 save() 。我决定在模型的方法中保存字段,例如:
def set_date_paid(self,date_str):
self.date_paid = date_str
self.save(update_fields=["date_paid"])

结果是我终于得到一个错误,我的数据无效。

我的错误是我希望得到验证错误。但正如文章和手册指出的那样,情况并非如此。

一旦我知道了这一点,我就确保我将数据筛选为正确的格式并且数据保存得很好。我没有回到在 View 中执行 save() 但我坚持使用模型工作,即使是单字段更新。我还不是很“pythonic”,所以我相信有更好的方法,但我希望这对你有帮助。

关于Django save() 方法不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50782502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com