嗯,由于某种原因,即使在最简单的模型上,我似乎也无法让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() …Run Code Online (Sandbox Code Playgroud)