0

I have a problem with a form validation in Django. I have this in views.py:

def n_visita(request, id_c):
    if request.method == 'POST':
        formulario = FormularioVisita(data=request.POST)
        if formulario.is_valid():
            ···
            save_form.save()
            return HttpResponseRedirect('/')
    else:
        formulario = FormularioVisita(idc=id_c)
        ···
        return...

And this in forms.py:

class FormularioVisita(ModelForm):
    class Meta:
        model = Visita

    def __init__(self, idc=None, *args, **kwargs):
        super(FormularioVisita, self).__init__(*args, **kwargs)
        self.fields["IdCita"].queryset = Citas.objects.filter(id=idc,Dia=date.today(),espera=True,estado=True)

This returns a single registry (because I give the "id"), show that option in the "select", all works fine. But when I do click in "Save" button Django say "Choose a valid option" and not show me one option in "select". What can I do? I need change the field to show the unique option of the queryset returns?

1 Answer 1

1

You need to pass in idc when you instantiate the form in the POST block too.

3
  • formulario = FormularioVisita(data=request.POST, id=id_c) is that correct? Mar 19, 2015 at 0:20
  • 1
    formulario = FormularioVisita(data=request.POST, idc=id_c)
    – Bang Dao
    Mar 19, 2015 at 4:18
  • Ok, that's right, but do not work. Django still says the same message. Mar 19, 2015 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.