gpt4 book ai didi

python - Django F 似乎不起作用?

转载 作者:行者123 更新时间:2023-12-01 23:23:27 24 4
gpt4 key购买 nike

嗯,出于某种原因,即使在最简单的模型上,我似乎也无法让 F 正常工作。这里是 Django 1.9.x。

最简单的形式,TestAccount

class TestAccount(models.Model):
decimal = models.DecimalField(max_digits=5, decimal_places=2)
integer = models.IntegerField()



In [1]: ta = TestAccount()

In [2]: ta.integer = 1

In [3]: ta.decimal = 1

In [4]: ta.save()

In [5]:

In [5]:

In [5]: ta
Out[5]: <TestAccount: TestAccount object>

In [6]: ta.id
Out[6]: 1L

In [7]: from django.db.models.expressions import F

In [8]: ta = TestAccount.objects.get(id=1)

In [9]: ta.integer = F('integer') + 1

In [10]: ta.save()
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
<ipython-input-10-6e9eda341b34> in <module>()
----> 1 ta.save()

/usr/lib/python2.7/site-packages/django/db/models/base.pyc in save(self, force_insert, force_update, using, update_fields)
706
707 self.save_base(using=using, force_insert=force_insert,
--> 708 force_update=force_update, update_fields=update_fields)
709 save.alters_data = True
710

/usr/lib/python2.7/site-packages/django/db/models/base.pyc in save_base(self, raw, force_insert, force_update, using, update_fields)
730 if not meta.auto_created:
731 signals.pre_save.send(sender=origin, instance=self, raw=raw, using=using,
--> 732 update_fields=update_fields)
733 with transaction.atomic(using=using, savepoint=False):
734 if not raw:

/usr/lib/python2.7/site-packages/django/dispatch/dispatcher.pyc in send(self, sender, **named)
190
191 for receiver in self._live_receivers(sender):
--> 192 response = receiver(signal=self, sender=sender, **named)
193 responses.append((receiver, response))
194 return responses

/media/sf_helium/build/helium/internal/signals.pyc in validate_model(sender, **kwargs)
12 def validate_model(sender, **kwargs):
13 if 'raw' in kwargs and not kwargs['raw']:
---> 14 kwargs['instance'].full_clean()
15
16 @receiver(pre_delete)

/usr/lib/python2.7/site-packages/django/db/models/base.pyc in full_clean(self, exclude, validate_unique)
1142
1143 if errors:
-> 1144 raise ValidationError(errors)
1145
1146 def clean_fields(self, exclude=None):

ValidationError: {'integer': [u"'F(integer) + Value(1)' value must be an integer."]}

但是根据这个:https://docs.djangoproject.com/en/1.9/ref/models/instances/#updating-attributes-based-on-existing-fields它应该有效...

我不知道为什么 F 没有被排除在验证之外。应该是这样,Django 应该只创建一个查询来更新它。

最佳答案

这在纯 Django 中工作得很好。您的情况的问题是您有一个试图执行此操作的 pre_save 信号的监听器(在 helium.internal.signals 中):

def validate_model(sender, **kwargs):
if 'raw' in kwargs and not kwargs['raw']:
kwargs['instance'].full_clean()

Model.full_clean 期望模型中的每个字段都有一堆值,但在这种情况下,您的一个字段不是一个值,而是一个 CombinedExpression ,它没有'尚未被评估,并且只有当 Django 写入数据库时​​才会被评估。这会导致错误。

IMO,您要么需要执行自己的验证来实现 full_clean 的逻辑并处理 Expression,或者您需要从 full_clean 中排除包含表达式的字段.

关于python - Django F 似乎不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38034414/

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