I've send this post this morning:

Hi

I have a formset and I'm passing initial data:
  
  DeletionFormset = formset_factory(deletionForm, extra=0)
  formset = DeletionFormset(initial=make_initial_data(instance))

The initial data arrives to the constructor fine. 
When the construct bilds the forms it overrides a ChoiceField with a tuple 
wich comes on the make_initial_data return

def make_initial_data(i):
    schools = School.objects.exclude(id=i.id)
    s = []
    for school in schools:
        s.append((school.id, school.name))
    n_schools = tuple(s)
    
    pupils = Pupil.objects.filter(school=i)
    ret = []
    for pupil in pupils:
        ret.append({
            'pupil_id': pupil.id,
            'pupil': pupil.user.get_full_name(),
            'school': n_schools,
        })
    return ret

So far all works fine.

This is my form used in the formset:

class deletionForm(forms.Form):
  pupil_id = forms.IntegerField(widget=forms.HiddenInput)
  pupil = forms.CharField()
  action = forms.ChoiceField(widget=forms.RadioSelect(), 
choices=(('D',_('Delete')),('M',_('Move'))))
  school = forms.ChoiceField()
    
  def __init__(self, *args, **kwargs):
      super(deletionForm, self).__init__(*args, **kwargs)
      self.base_fields['school'] = 
forms.ChoiceField(choices=self.initial['school'])
      print self.base_fields['school'].choices
    
For testing it return a tuple with two indexes. In this print it shows the 
correct data in both select input, but in the form it only appears in the last 
select.

This the output for the print command: (school's names)
[(1, u'ABC'), (3, u'xpto')]
[(1, u'ABC'), (3, u'xpto')]


This is the select output in the rendered html:
...
<td><select name="form-0-school" id="id_form-0-school">
</select></td>
....
 ...                    
<td><select name="form-1-school" id="id_form-1-school">
<option value="1">HCT</option>
<option value="3">xpto</option>
</select></td>
...

What am I missing?

Thanks


After I sent it, somehow, it starts working by itself.
Now it doesn't work again. I've changed the form to only create the school 
field in the init but still doesn't work

My current code is now like this:
class deletionForm(forms.Form):
    pupil_id = forms.IntegerField(widget=forms.HiddenInput)
    pupil = forms.CharField()
    action = forms.ChoiceField(widget=forms.RadioSelect(), 
choices=(('D',_('Delete')),('M',_('Move'))))
    #school = forms.ChoiceField()
    
    def __init__(self, *args, **kwargs):
        super(deletionForm, self).__init__(*args, **kwargs)
        print self.initial
        if self.initial:
            m_choices = self.initial['school']
            self.base_fields['school'] = forms.ChoiceField(choices = m_choices)

If I print in the end of __init__ it's correct. But in the rendered html 
doesn't appear nothing in the first select.  But like this, because I'm only 
creating the school field in the init, in the first formset doesn't appear the 
select. Before it was empty, now it doesn't exists.

I spent all the afternoon googling for this, somebody know whats happening?

By the way, how can I put a selected attribute in one of the choices? In 
googling it says to put what I want to be default in value but doesn't do 
anything

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to