为什么所选表单的字段在数据库中返回值“ none”和“ null”?

时间:2019-07-31 14:45:18

标签: django django-models django-forms

我试图在两个字段中保存一个值,但是在保存它们时,我将该值更改为none,并且数据库还向我显示了null

我试图将括号改为方括号,看看是否将表格的两个值都取到了数据库中,但是我不太了解form.cleaned_data()和{{1}之间的区别},但使用方括号会产生错误。

views.py

form.cleaned_data[]

我的models.py

def solit(request):
    if request.method == 'POST' and request.is_ajax():
        form = addiForm(request.POST)
        if form.is_valid():
            peticion = form.save(commit=False)
            peticion.usuario = request.user
            peticion.save()
            peticion.usuario.d_pendientes = form.cleaned_data.get('d_pendientes')
            peticion.usuario.h_pendientes = form.cleaned_data.get('h_pendientes')
            peticion.usuario.save() 
            print (peticion.usuario.d_pendientes)
            print (peticion.usuario.h_pendientes)  
            return JsonResponse({'status': 'true', 'msg': 'Procesado Correctamente'})
        else:
            return JsonResponse({'status': 'false', 'msg': 'Los datos no son validos'})
    form = addiForm()
    return render(request, 'plantillas/adicionar.html', {'form':form})

forms.py

class Usuarios(AbstractUser):
    numero_empleado = models.IntegerField(null= True, blank= True)
    area = models.CharField(max_length = 200, null= True, blank= True)
    d_pendientes = models.IntegerField(null= True, blank= False)
    h_pendientes = models.IntegerField(null= True, blank= False)
    f_init = models.DateField(max_length = 200,null= True, blank= True)
    init_vac = models.DateField(max_length = 200, null= True, blank= True)
    fin_vac = models.DateField(max_length = 200, null= True, blank= True)
    ul_vac_tomadas = models.IntegerField(null= True, blank= True)


class Peticion(models.Model):
    solit_choices = (
    ('Adicionar','Adicionar'),
)
    solicitudes_id = models.AutoField(primary_key=True)
    usuario = models.ForeignKey(Usuarios, on_delete=models.CASCADE, null=True, blank=True)
    petit = models.CharField(max_length = 255, choices=solit_choices, null=True, blank=False)
    fec = models.DateTimeField(auto_now=True)
    razon = models.TextField(max_length=255, null=True, blank=True)
    periodo_init = models.DateField(max_length = 200, null=True, blank=True)
    periodo_fin = models.DateField(max_length = 200, null=True, blank=True)
    dias_adicion = models.IntegerField(null=True, blank=False)
    horas_adicion = models.FloatField(null=True, blank=False)

在控制台中,我看到他正在接收POST请求,但它还会打印值class addiForm(forms.ModelForm): class Meta: """Formulario de solicitud""" model = Peticion fields = [ 'solicitudes_id', 'petit', 'razon', 'periodo_init', 'periodo_fin', 'dias_adicion', 'horas_adicion', ] 而不是我在表单输入中输入的值。

1 个答案:

答案 0 :(得分:0)

这样,您可以轻松保存表单值

 if user_form.is_valid() and profile_form.is_valid():
                    user = user_form.save()

https://docs.djangoproject.com/en/2.2/ref/forms/api/#django.forms.Form.cleaned_data