>On Thu, Mar 25, 2010 at 2:00 AM, mjlissner <[email protected]> wrote:
> I'll make things more concrete. I have the following in my model
> (which is made into a ModelForm):
> class UserProfile(models.Model):
>    barmembership = models.ManyToManyField(BarMembership,
>        verbose_name="the bar memberships held by the user",
>        blank=True,
>        null=True)
>
> In my view, I have some code that looks like this:
>        userProfile = request.user.get_profile()
>        bar_memberships = userProfile.barmembership.all()
>
>        profileForm = ProfileForm(
>        initial = {'barmembership' : [bar_memberships]})


I don't know why you're not using the "instance" argument to populate the form

profileForm = ProfileForm(instance=userProfile)


but you need to give the ModelForm a list of PKs for the many-to-many-field:

bar_memberships = [obj.pk for obj in ob.barmembership.all()]
profileForm = ProfileForm( initial = {'barmembership' : bar_memberships})

(see the model_to_dict code in django/forms/models.py )

And force the refresh on the browser if it looks like it's not working
:) (at least FF has a strange
habit of keeping selections when reloading a form, ignoring the
initial selected items on the HTML).

hth,
Nuno

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

Reply via email to