为什么 Django form.save() returns object(None)?
Why Django form.save() returns object(None)?
from django.shortcuts import render
from django.forms import forms
from .models import Chart, ChartData
from .forms import ChartForm, ChartDataForm
import datetime
def home(request):
chart_form = ChartForm(request.POST)
chart_data_form = ChartDataForm(request.POST)
context = {
'chart_form': chart_form,
'chart_data_form': chart_data_form,
}
if chart_form.is_valid():
chart_obj = chart_form.save()
chart = Chart.objects.get(pk=23)
print(chart_obj)
print(chart)
return render(request, 'charts/home.html', context)
chart_form.save() returns "图表对象(None)
我最终必须将 chart_obj 传递给外键的另一个模型,但现在它在那里刹车并出现错误 - save() 被禁止以防止由于未保存的相关数据丢失对象.
在管理站点中,我看到对象已创建。
models.py低于
import datetime
from django.db import models
from django.forms import DateTimeField
# Create your models here.
class Chart(models.Model):
id = models.IntegerField(primary_key=True)
type = models.CharField(max_length=10)
chartname = models.CharField(max_length=20, null=True)
class ChartData(models.Model):
dataY = models.IntegerField()
dataX = models.DateTimeField()
chart = models.ForeignKey(Chart, on_delete=models.CASCADE)
forms.py以下
from django import forms
from .models import Chart, ChartData
class ChartForm(forms.ModelForm):
class Meta:
model = Chart
fields = ['type','chartname']
class ChartDataForm(forms.ModelForm):
class Meta:
model = ChartData
fields = ['dataY']
您应该在保存对象时使用 AutoField
来填充模型对象,因此:
class Chart(models.Model):
id = models.<strong>AutoField(</strong>primary_key=True<strong>)</strong>
type = models.CharField(max_length=10)
chartname = models.CharField(max_length=20, null=True)
或者只是没有 id
字段:
class Chart(models.Model):
type = models.CharField(max_length=10)
chartname = models.CharField(max_length=20, null=True)
这里你不需要明确定义id,因为Django会为你做同样的事情。如果你想将主键重命名为其他东西,你可以使用下面的方法。
from django.shortcuts import render
from django.forms import forms
from .models import Chart, ChartData
from .forms import ChartForm, ChartDataForm
import datetime
def home(request):
chart_form = ChartForm(request.POST)
chart_data_form = ChartDataForm(request.POST)
context = {
'chart_form': chart_form,
'chart_data_form': chart_data_form,
}
if chart_form.is_valid():
chart_obj = chart_form.save()
chart = Chart.objects.get(pk=23)
print(chart_obj)
print(chart)
return render(request, 'charts/home.html', context)
chart_form.save() returns "图表对象(None)
我最终必须将 chart_obj 传递给外键的另一个模型,但现在它在那里刹车并出现错误 - save() 被禁止以防止由于未保存的相关数据丢失对象.
在管理站点中,我看到对象已创建。
models.py低于
import datetime
from django.db import models
from django.forms import DateTimeField
# Create your models here.
class Chart(models.Model):
id = models.IntegerField(primary_key=True)
type = models.CharField(max_length=10)
chartname = models.CharField(max_length=20, null=True)
class ChartData(models.Model):
dataY = models.IntegerField()
dataX = models.DateTimeField()
chart = models.ForeignKey(Chart, on_delete=models.CASCADE)
forms.py以下
from django import forms
from .models import Chart, ChartData
class ChartForm(forms.ModelForm):
class Meta:
model = Chart
fields = ['type','chartname']
class ChartDataForm(forms.ModelForm):
class Meta:
model = ChartData
fields = ['dataY']
您应该在保存对象时使用 AutoField
来填充模型对象,因此:
class Chart(models.Model):
id = models.<strong>AutoField(</strong>primary_key=True<strong>)</strong>
type = models.CharField(max_length=10)
chartname = models.CharField(max_length=20, null=True)
或者只是没有 id
字段:
class Chart(models.Model):
type = models.CharField(max_length=10)
chartname = models.CharField(max_length=20, null=True)
这里你不需要明确定义id,因为Django会为你做同样的事情。如果你想将主键重命名为其他东西,你可以使用下面的方法。