0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

データベースの現在値を元にフォームの入力値をチェックする方法

Posted at

フォームに入力された値をチェックするときに、すでにDBに保存済みの値と比較する方法がわからなかったのでメモ。

self.instance.モデルフィールド名

で取得できる。

models.py

class Order(models.Model):
    quantity = models.IntegerField(verbose_name='注文数')

forms.py

def clean_quantity(self):
    new_val = self.cleaned_data['quantity']
    if new_val > self.instance.quantity + 10:
        raise forms.ValidationError("注文数の追加は10までです")
    return new_val
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?