メモ〜cleaned_dataと外部キー
2694 ワード
今回の結論
ModelForm
クラスが外部キー由来のフィールドを持っている場合、cleaned_data
にもprimary-key
ではなくインスタンスそのもの
が入る。
具体例
models.py
class Book(models.Model):
title = models.CharField(max_length=30)
author = models.Foreign_key(Author, on_delete=models.CASCADE)
forms.py
class BookForm(ModelForm):
class Meta:
fields = "__all__"
def clean_author(self):
author = self.cleaned_data.get("author") # ここにはインスタンスそのものが入っている
if author.pk == 1:
raise forms.ValidationError("テストユーザーを著者として登録しないでください")
return author
Author And Source
この問題について(メモ〜cleaned_dataと外部キー), 我々は、より多くの情報をここで見つけました https://qiita.com/keishi04hrikzira/items/71b6cce7fa0c48db7db0著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .