我有一个错误,在尝试将数据保存到数据库中时,未保存数据,在填充数据时,似乎已经保存了数据,但这并没有做到,因为每个字段经过验证,当我发送时没有错误,这是基于类的视图。
@pandas_udf(mySchema, PandasUDFType.GROUPED_MAP)
def forecast(df):
model = Prophet(
growth="linear",
interval_width=0.10,
seasonality_mode="multiplicative",
yearly_seasonality=False,
weekly_seasonality=False,
daily_seasonality=False,
).add_seasonality(name="monthly", period=12 * 30.5, fourier_order=12)
model.fit(df.loc[:, ["ds", "y"]])
futper = model.make_future_dataframe(periods=12, freq="M")
results_pd = model.predict(futper)
results_pd = pd.concat([results_pd, df["prod"]], axis=1)
return pd.DataFrame(results_pd, columns=mySchema.fieldNames())
results = sparkdf.groupby('prod').apply(forecast)
results.columns
这是表格
from django.shortcuts import render, redirect
from django.views.generic import View
from .models import Comment
from .forms import CommentForm
class IndexView(View):
model = Comment
template_name = 'home/index.html'
form_class = CommentForm
def get_queryset(self):
return self.model.objects.all().order_by('-pk')
def get_context_data(self, **kwargs):
context = {}
context['commentaries'] = self.get_queryset()
context['form'] = self.form_class
return context
def get(self, request, *args, **kwargs):
return render(request, self.template_name, self.get_context_data())
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
form.save()
form.errors.as_json()
return redirect('home:index')
这是模型
from django import forms
from django.urls import reverse_lazy
from .models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['name', 'comment']
labels = {
'name': 'Nombre',
'comment': 'Comentario'
}
widgets = {
'name': forms.TextInput(
attrs={
'class': 'form-control',
'placeholder': 'Ingrese su Nombre',
'id': 'name',
}
),
'comment': forms.Textarea(
attrs={
'class': 'form-control',
'placeholder': 'Ingrese su comentario',
'id': 'comment',
}
)
}
这是html
from django.db import models
class Comment(models.Model):
name = models.CharField(max_length=50)
comment = models.TextField(max_length=250)
created_at = models.DateField(auto_now=True)
def __str__(self):
return self.name
def save(self):
self.name = self.name.capitalize()
我已经尝试过放置form.errors.as_json()来知道错误是什么,但是它什么也不返回
答案 0 :(得分:0)
您需要使用保存方法调用super
def save(self, **kwargs):
self.name = self.name.capitalize()
super().save(**kwargs)
基类models.Model
的save方法是将对象插入数据库的方法。通过覆盖它而不调用super
,可以防止对象被保存