On Oct 16, 4:36 am, Tim Valenta <tonightslasts...@gmail.com> wrote: > I have a model like such: > class Response(models.Model): > raw_text = models.TextField() > respondent = models.ForeignKey('auth.User') > > Response has a related model attached to it: > class Code(models.Model): > response = models.ForeignKey(Response) > coded_answer = models.IntegerField() > > The idea is that Responses come in as typed text, and then are > processed and assigned standardized Codes to represent the meaning > contained in the text. > > Now, it greatly simplifies my life if I can use a normal formset to > represent the main Response objects gathered on a page for processing, > using the 'queryset' parameter to scope the Responses being handled. > I'm making use of the admin's filtered select widget in a ModelForm > for the Response model, like so: > class ResponseCodingForm(forms.ModelForm): > codes = forms.MultipleChoiceField(choices=[yada yada], > widget=FilteredSelectMultiple('Codes', false)) > class Meta: > model = Response > fields = ['raw_text'] > > As you can see, the form only represents the "raw_text" field, but > adds a virtualized field called "codes" to the mix, which I want to > manage myself. That is, I want to inject data values into it when I > create my formset, as you will soon see. This approach works great > for when I render the formset, properly placing a field for "codes" on > my forms. Naturally, the value of this field gets POSTed back to my > view, which I happily read from my form's cleaned_data attribute, > processing as I see fit. > > But when it comes time to instantiate my formset, I can't find a way > to pump my own values into this "codes" field on the form. Here is > the relevant part of the view: > responses = Response.objects.filter( yada yada) > ResponseCodingFormSet = modelformset_factory(Response, > form=ResponseCodingForm, extra=0) > initial = [{ > 'codes': list(response.code_set.values_list('id', flat=True)), > } for response in responses] > > if request.method == 'POST': > # handle validation and save operations > else: > formset = ResponseCodingFormSet(queryset=responses, > initial=initial) > > I realize that what I'm doing could be handled by some inline > formsets, but I really wanted to leverage that FilteredSelectMultiple > widget to represent my related model association. > > No matter what I do, the formset's value for any of the objects' > "codes" field is empty.
That's strange. I've just run the same code myself and have found that the 'codes' field was populated properly, with the IDs of the associated objects selected. The only thing I can think of is that the choices for the field are not being set correctly to start with - in the bit where you write `(choices=[yada yada]`, what is 'yada yada'? Obviously, this must contain *all* possible values for the codes across all responses, so that the relevant ones can be selected for each item in the formset. -- DR. -- 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.