All pastes #984067 Raw Edit

Mine

public text v1 · immutable
·published 2008-04-13 22:05 UTC
rendered paste body
# ModelForms are a much better approach, now that they exist.
# However here's an fix to the above workflow:

def save_presentation(request, form, user):
    ...

    # All this can be replaced with the shorthand version below

    # pres = Presentation.objects.create(
    #     presenter=user,
    #     cat = Category(form.cleaned_data['category']),
    #     short_abstract = form.cleaned_data['short_abstract'],
    #     title = form.cleaned_data['title'],
    #     status = Status(name='Pending'),
    # )

    pres = Presentation(**form.cleaned_data)
    pres.save()


    # Note: I don't know, off-hand, if the above shorthand will Just Work with the M2M field.
    # If not you'll need the code below too:


    # ModelChoiceField cleaned_data returns actual model objects
    # so the way to loop through and add them to an existing model is:
    for i in form.cleaned_data['audience_list']:
        pres.audience.add(i)