创建初始数据(CreateView)后,我继续更新更多数据(UpdateView),但在更新新数据时我得到以下答案。我附上代码的相关部分:
ValueError at /preinscripcion/padres/63
Cannot assign None: "Alumni.cycle" does not allow null values.
Request Method: POST
Request URL: http://decroly.administracionescolarmexico.com/preinscripcion/padres/63
Django Version: 1.5
Exception Type: ValueError
Exception Value:
Cannot assign None: "Alumni.cycle" does not allow null values.
Exception Location: /home/fbenavides/webapps/django/lib/python2.7/django/db/models/fields/related.py in __set__, line 401
Python Executable: /usr/local/bin/python
Python Version: 2.7.5
Model.py
# -*- coding: utf-8 -*-
# Create your models here
from django.db import models
from django.forms import ModelForm
from django.conf import settings
from django.core.files.storage import FileSystemStorage
import os.path
import datetime
# Create your models here
class ScholarCycle(models.Model):
"""
ScholarCycle
Yearly School Cycle (e.g. 2012-2013)
"""
cycle = models.CharField(blank=False, max_length=9, verbose_name=u'Ciclo Escolar')
def __unicode__(self):
return u'%s' % self.cycle
class Meta:
db_table = u'Ciclo Escolar'
ordering = ['id', 'cycle']
verbose_name = u'Ciclo Escolar'
verbose_name_plural = u'Ciclos Escolares'
class Admin:
pass
fs_photos = FileSystemStorage(location='/media/user/photos')
fs_docs = FileSystemStorage(location='/media/user/docs')
class Alumni(models.Model):
"""
"""
cycle = models.ForeignKey(ScholarCycle, unique=False, null=False, blank=True, verbose_name=u'Ciclo Escolar')
url.py
urlpatterns = patterns('',
url(r'^lista/$', views.AlumniList.as_view(), name='alumni-lista'),
url(r'^lista/(?P<page>\d+)$', views.AlumniRedirectMostrar.as_view(), name='alumni-redirect-mostrar'),
url(r'^mostrar/(?P<pk>\d+)$', views.AlumniRetrieve.as_view(), name='alumni-mostrar'),
url(r'^mostrar/padres/(?P<pk>\d+)$', views.AlumniRetrievePadres.as_view(), name='alumni-mostrar-padres'),
url(r'^mostrar/autorizado/(?P<pk>\d+)$', views.AlumniRetrieveAutorizado.as_view(), name='alumni-mostrar-autorizado'),
url(r'^mostrar/finanzas/(?P<pk>\d+)$', views.AlumniRetrieveFinanzas.as_view(), name='alumni-mostrar-finanzas'),
url(r'^mostrar/documentos/(?P<pk>\d+)$', views.AlumniRetrieveDocumentos.as_view(), name='alumni-mostrar-documentos'),
url(r'^inscripcion/$', views.AlumniRegister.as_view(), name='alumni-inscripcion'),
url(r'^inscripcion/padres/(?P<pk>\d+)$', views.AlumniRegisterPadres.as_view(), name='alumni-inscripcion-padres'),
url(r'^inscripcion/autorizado/(?P<pk>\d+)$', views.AlumniRegisterAutorizado.as_view(), name='alumni-inscripcion-autorizado'),
url(r'^inscripcion/finanzas/(?P<pk>\d+)$', views.AlumniRegisterFinanzas.as_view(), name='alumni-inscripcion-finanzas'),
url(r'^inscripcion/documentos/(?P<pk>\d+)$', views.AlumniRegisterDocumentos.as_view(), name='alumni-inscripcion-documentos'),
url(r'^preinscripcion/$', views.AlumniPreinscripcion.as_view(), name='alumni-preinscripcion'),
url(r'^preinscripcion/padres/(?P<pk>\d+)$', views.AlumniPreinscripcionPadres.as_view(), name='alumni-preinscripcion-padres'),
url(r'^preinscripcion/autorizados/(?P<pk>\d+)$', views.AlumniPreinscripcionAutorizados.as_view(), name='alumni-preinscripcion-autorizados'),
url(r'^preinscripcion/finanzas/(?P<pk>\d+)$', views.AlumniPreinscripcionFinanzas.as_view(), name='alumni-preinscripcion-finanzas'),
views.py
class AlumniPreinscripcion(LoginRequiredMixin, CreateView):
model = models.Alumni
form_class = forms.AlumniFormPreinscripcion
context_object_name = 'alumno'
template_name = 'alumni_preinscripcion.html'
def get_initial(self):
initial = super(AlumniPreinscripcion, self).get_initial()
initial['cycle'] = models.ScholarCycle.objects.get(cycle__exact='2014-2015')
return initial
def get_success_url(self, **kwargs):
return reverse('alumni-preinscripcion-padres', kwargs={'pk':self.object.id})
class AlumniPreinscripcionPadres(LoginRequiredMixin, UpdateView):
model = models.Alumni
form_clas = forms.AlumniFormPreinscripcionPadres
context_object_name = 'alumno'
template_name = 'alumni_preinscripcion_padres.html'
def get_sucess_url(self, **kwargs):
return reverse('alumni-preinscripcion-autorizados', kwargs='pk':self.object.id})
alumni_preinscripcion_padres.html
{% extends "preinscripcion.html" %}
{% load i18n %}
{% load l10n %}
{% block content %}
<h2>Padres</h2>
<form action="" method="post" enctype="multipart/form-data" accept-charset="utf-8">
{% csrf_token %}
<fieldset>
<legend><strong> Papá </strong></legend>
<div class="row-fluid">
<div class="span2">
<p>Nombre:</p>
</div>
<div class="span10">
<p>{{ form.father_name }} {{ form.father_familynames }}</p>
</div>
</div>
<br />
<p><input type="submit" value="Persona(s) Autorizada(s) →" /></p>
</form>
{% endblock %}
答案 0 :(得分:0)
校友模型上的cycle
字段定义为null=False, blank=True
。你可能想改变它。我无法告诉您正确的值,因为这取决于您的应用程序逻辑,但这些值与您的表单视图不一致。来自documention:
null纯粹与数据库相关,而空白与验证相关。 如果字段为空= True,则表单验证将允许输入 空值。如果字段为空= False,则需要该字段。
阅读整个部分。一旦您了解了模型字段的正确设置,表单和基于类的视图很可能会正常工作。 (仅供参考,你的CreateView工作的原因是因为你专门设置满足空标准的initial['cycle']
而你没有在UpdateView中这样做,而更新表格认为可以有空/空值。)
更新:AlumniPreinscripcionPadres
视图中有两个拼写错误,get_sucess_url()
和form_clas
,都错过了一封信。