Yes, I agree with what you are saying about self._initial_form_count being set to a number greater than zero due to me using initial in my code that creates the formset, as follows:
def makeTaskDetailFormSetForCreate(tileList, data=None): TaskDetailFormSet = modelformset_factory(TaskDetail, form=TaskDetailForm, formset=TaskDetailCustomBaseFormSetForCreate) taskDetailFormSet = TaskDetailFormSet(data=data, tiles=tileList, initial=[{"tile":tile.id} for tile in tileList]) return taskDetailFormSet But if I don't use initial - how do I create the initial values for the forms? I thought using initial is the "right" way to do it? And of course, as I described in the formset initialization thread, when I don't use initial and I instead try setting it in the init for the form, like this: class TaskDetailForm(forms.ModelForm): class Meta: model=TaskDetail exclude=('task') def __init__(self, tile, *args, **kwargs): super(TaskDetailForm, self).__init__(*args, **kwargs) self.fields['tile'].initial = tile.id I seem to have a different set of problems related to cleaned_data not getting set correctly. I guess these two threads are converging - I'll look for your response there. By the way, you may have noticed that in some cases my code has TaskTileDetail while in others it is TaskDetail. I tried shortening the name for the purpose of the posting, and ended up missing some when I did the cut and paste, sorry for any added confusion from that. Margie On Mar 4, 6:09 pm, Malcolm Tredinnick <malc...@pointy-stick.com> wrote: > On Wed, 2009-03-04 at 11:44 -0800, Margie wrote: > > Hi Malcolm. So after reviewing your pointers, I had high hopes of > > being able to move to using the modelformset_factory, but I still seem > > to have problems. The problem I run into is that when _construct_form > > (the one in django/forms/models.py) is called, it tries to do this: > > > if i < self._initial_form_count: > > kwargs['instance'] = self.get_queryset()[i] > > > I have no objects of my model yet and get_queryset() returns > > self._queryset, which is []. The above [i] index then causes a 'list > > index out of range exception'. I think this was what I first > > encountered which made me think that I had to have a queryset to use > > modelformset_factory. > > That second line of code will only be executed if > self._initial_form_count is greater than 0. You aren't providing any > initial forms, so it should be zero. > > Looking at the source (it's set in the BaseFormset.__init__ method), it > will only be non-zero if you supply initial data, which kind of makes > sense, since providing initial data for "extra" forms would mean you'd > always end up creating a new record every time you viewed and submitted > that form. I can see why it's set up that way, although it confirms my > wish to spend a couple of days going over the formsets documentation > with a two-by-four. > > I think it means that supplying this tile id via initial data is > probably not a great idea, as it will inadvertently create new records > own the track. I'd kind of been thinking that approach might have been a > little clunky, anyway, so this backs up that hunch. I'll put some more > details in the formset initialization thread, because it's probably more > appropriate there. > > Regards, > Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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 -~----------~----~----~----~------~----~------~--~---