Django save()方法没有保存

问题描述 投票:1回答:1

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

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

但是,它不保存到数据库并且没有错误。

你能明白为什么不能保存吗?有没有更好的方法来调试保存方法?我环顾四周,看不到任何东西,不知道为什么不能保存是非常令人沮丧的。

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

信号

@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()

现在似乎使它工作:-)

django django-models django-signals
1个回答
0
投票

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

https://realpython.com/modeling-polymorphism-django-python/说:

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

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

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

使用这种方法,我仍然在调用方法的视图中执行save()。我决定在模型的方法中保存字段,如:

def set_date_paid(self,date_str):
    self.date_paid = date_str
    self.save(update_fields=["date_paid"])

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

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

一旦我知道这一点,我确保我正在筛选数据到正确的格式,数据保存得很好。我没有回去在视图中执行save(),但即使是单个字段更新,我仍然坚持使用模型。我还不是很“pythonic”,所以我确信有更好的方法,但我希望这会对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.