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.

On Oct 14, 3:09 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Oct 14, 10:32 am, Tim Valenta <tonightslasts...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > This is driving me mad, so I must ask the community, in hopes of a
> > workaround:
>
> > I've got a simple formset of models on a page, generated by
> > modelformset_factory.  This particular page will never create forms--
> > it only modifies existing ones.  Therefore the queryset I pass into
> > the formset constructor gets built, and everything works great.
>
> > I've added a field to the formset, but Django seems perfectly happy to
> > totally ignore whatever initial data I want to supply for this
> > 'virtual' field that I've added.  It seems that I've got absolutely no
> > mechanism for setting a dynamically-generated value as a pre-computed
> > field value.
>
> > My situation is that I want to represent an many-to-many relationship
> > on this 'virtual' field, which doesn't actually exist on my model.
> > When the form is submitted, I can easily intercept the values sent and
> > do what I will with them.  However, when the page refreshes after a
> > submission, I cannot for the life of me inject this data back into the
> > form.  As of right now, my select-multiple widget is quite blank, no
> > matter what I do.
>
> > Help?  I've literally been at this for hours off and on.
>
> Can you show some code? It would help to understand what you're doing.
> --
> 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.

Reply via email to