SOLVED: Read Daniel Roseman's neat answer. It worked perfectly.
I am not a very expert on Django: sorry if my question has a trivial answer.
I am using Django dev (1.8?).
My application manages the entries of certain DATE
type which needs to be validated and coherent. Therefore in
views/create_fest.py
I have as follows:
class Formulario_nuevo_festivo(forms.ModelForm):
class Meta:
model = Festivo
fields = ('nombre_festivo','fecha_unica','fecha_inicio','fecha_fin')
def clean(self):
cleaned_data=super(Formulario_nuevo_festivo,self).clean()
fecha_unica =cleaned_data.get("fecha_unica","")
fecha_inicio =cleaned_data.get("fecha_inicio","")
fecha_fin =cleaned_data.get("fecha_fin","")
if fecha_unica and fecha_inicio and fecha_fin:
raise forms.ValidationError(u"some message")
elif not fecha_unica and not fecha_inicio and not fecha_fin:
raise forms.ValidationError(u"some message")
elif (fecha_unica and (fecha_inicio!=None or fecha_fin!=None)) or (fecha_inicio and not fecha_fin) or (fecha_fin and not fecha_inicio):
raise forms.ValidationError(u"some message.")
else:
if (fecha_unica and (fecha_inicio==None or fecha_fin==None)):
pass
elif fecha_inicio > fecha_fin or fecha_inicio==fecha_fin:
raise forms.ValidationError(u"some message.")
else:
pass
return cleaned_data
def page(request):
if request.POST:
form = Formulario_nuevo_festivo(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse_lazy('listado_festivos'))
else:
return render(request, 'crear_festivo.html',{'form':form})
else:
form=Formulario_nuevo_festivo()
return render(request,'crear_festivo.html',{'form':form})
which works perfectly: the validation works fine and the user can only create an object by accomplishing with the validation-
The problem comes when i am using UPDATEVIEW to manage changes to this model:
Here comes a piece of code inside of views.py
snippet:
...
class FestivoUpdateView(UpdateView):
model = Festivo
fields = ['nombre_festivo','fecha_unica','fecha_inicio','fecha_fin']
template_name = "editar_festivo.html"
success_url = reverse_lazy('listado_festivos')
def post(self, request, *args, **kwargs):
if "cancel" in request.POST:
self.object = self.get_object()
url = self.get_success_url()
return HttpResponseRedirect(url)
else:
return super(FestivoUpdateView, self).post(request, *args, **kwargs)
...
The thing is that the user can enter whatever data into this UdateView with no kind of validation.
I have searched a lot (since I am not an english native speaker) but i am not having luck on finding an answer.
A lazy programmer would say "hey you could repeat the validation code in the views.py and do the validation again" but it would be against DRY philosophy and i am pretty sure there must be an easy way to force the UpdateView to use the create view validation.
What about if i put it someplace like "core/validate.py" and then import it as a function?
I have no clue how to solute this, any help would be appreciate.
Thanks ahead
You simply need to tell your view to use your form.
class FestivoUpdateView(UpdateView):
form_class = Formulario_nuevo_festivo
...etc...
Note there doesn't seem to be anything in your form that makes it solely for new festivals, so you may want to rename it to something more generic (and PEP8-compliant), such as FormularioFestivo.