I searched in the web for some example of setting a FK in a ModelForm 
(bound and unbound), but the examples I founded didn't worked for me. The 
best thing I think I've is this:

*forms.py*








*class RegistroComentarioForm(ModelForm):    def __init__(self, *args, 
**kwargs):        super(RegistroComentarioForm, self).__init__(*args, 
**kwargs)        # This is supossed to get all rows from a model. A 
"Comentario" has one "CategoriaComentario".        # Maybe I should do a 
filter here when I bound data, but the problem is in the tests...        
self.fields['categoria'] = ModelChoiceField(            
queryset=CategoriaComentario.objects.all().values_list('id', 'nombre'),     
       empty_label='...')    # bla bla bla Meta bla bla bla 
fields...tests.py*

class RegistroComentarioFormTestCase(TestCase):
    def test_registrar_comentario(self):
        # Registro_Comentario_Form_X.
        r_c_f_a = RegistroComentarioForm({
            # I tried "'categoria': 1", but nothing.
            'enunciado': 'Eres tan fea que sólo te guiñan un ojo los '
                         'francotiradores.'
        })

        # Here is the problem. This prints:
        # <select id="id_categoria" name="categoria">

        # <option value="" selected="selected">---------</option>

        # </select>

        # <textarea cols="40" id="id_enunciado" maxlength="1000" 
name="enunciado"
        # placeholder="Sólo caracteres alfanuméricos, sin espacios, máx. 1000" 
rows="10">
                 # Eres tan fea que sólo te guiñan un ojo los 
francotiradores.</textarea>

        for campo in r_c_f_a:
            print(campo)

        self.assertTrue(r_c_f_a.is_valid())


So I don't have any *CategoriaComentario* available. In the docs they don't 
say much about *ModelChoiceField*, so I just don't know what to do.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/bfdaa10a-cacb-4c00-9058-c5f28585f74e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to