簡體   English   中英

Django F似乎不起作用?

[英]Django F doesn't seem to work?

呵呵,由於某種原因,即使在最簡單的模型上,我似乎也無法正常運行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 ),該pre_save器試圖這樣做:

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排除包含表達式的字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM