首页 > 解决方案 > Is there anyways to store the data from the forms into variables without saving it to the database?

问题描述

I'm trying to save the data inside a form into variables without saving it to the database but I don't know how. I'm not even sure if it's possible.

标签: pythondjango

解决方案


ModelForm除非您明确调用它,否则不会保存其中的数据save,因此您可以自由地读取数据cleaned_data并根据需要使用它。如果您使用的是常规表单,则仅当您通过将数据复制到实例中来创建/更新模型实例时,它才会保存到数据库中cleaned_data

例如:

def some_view(request):
   form = MyForm(request.POST)
   if form.is_valid():
       foo = form.cleaned_data['foo']
       # Do what you want with `foo`. Save to session, print to console, etc.

推荐阅读