Hi all,

I have a view that takes data from a form and saves it in the db.  One
of the fields is a M2M linking to another model.  The record itself
saves fine, but no entry in the M2M join table is created.  I am
passing the form initial data for the M2M in the form of a list of id
numbers specifying which items are to be selected.  Sure enough the
items are properly selected in the 'choose many' box, but the join
table is never made.

If anybody can provide a hint I would really appreciate it; I have
been going over and over this looking for an error and it is starting
to drive me crazy!

This is my code:

#views.py (to_form is the list of id numbers specifying the items to
be saved in the M2M field)
--------------

if request.method == 'POST':
                f = InteractionForm(request.POST)
                if f.is_valid():

                        interaction = f.save(commit=False)
                        interaction.owner = request.user
                        interaction.save()
                        return 
HttpResponseRedirect('http://127.0.0.1:8000/contact/%d' %
to_form[0])

        else:
                f = InteractionForm(initial={'contacts': to_form})

                return render_to_response('addinteraction.html', {'form': f})

#models.py
---------------------

class Interaction(models.Model):
        owner = models.ForeignKey(User, editable=False)
        SORT_CHOICES = (
                ('CALL', 'Call'),
                ('EMAIL', 'Email'),
                ('MEET', 'Meeting'),
        )
        sort = models.CharField(choices=SORT_CHOICES, max_length=5)
        contacts = models.ManyToManyField(Contact)
        date = models.DateTimeField(auto_now_add=True)
        notes = models.CharField(max_length=20000)

        def __unicode__(self):
                return self.id

class InteractionForm(ModelForm):
        class Meta:
                model = Interaction

#addinteraction.html
--------------------------------

<html>
<head><title>Add Interaction</title></head>

<body>

<h1>Add Interaction</h1>


<form method="post" action="">
    {{ form }}
<input type='submit' value='Add'>


</form>
</body>
</html>


-- 
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