Hi all,

I have two models, a contact model:

class Contact(models.Model):
        company = models.ForeignKey(Company)
        first_name = models.CharField(max_length=200)
        last_name = models.CharField(max_length=200)
        title = models.CharField(max_length=200)
        office_telephone = models.CharField(max_length=20)
        dd_telephone = models.CharField(max_length=20)
        mobile_telephone = models.CharField(max_length=20)
        address_1 = models.CharField(max_length=200)
        address_2 = models.CharField(max_length=200)
        address_3 = models.CharField(max_length=200)
        city_county = models.CharField(max_length=200)
        post_code = models.CharField(max_length=8)
        fax = models.CharField(max_length=20)
        email_address = models.EmailField(max_length=75)

and an email model:

class Email(models.Model):
        contacts = models.ManyToManyField(Contact)
        date = models.DateTimeField(auto_now_add=True)
        notes = models.CharField(max_length=20000)

I have a view that shows the contact data for a particular contact
record, and I am putting a link on there that allows the user to
attach an email record to that contact.  I want the M2M field to be
initially set to the contact record they have come from:

def addcontactemail(request, contact_id):
        contact_id = int(contact_id)
        contact = Contact.objects.filter(id=contact_id)
        contact = contact[0]
        if request.method == 'POST':
                f = EmailForm(request.POST)
                if f.is_valid():
                        f.save()
                        return 
HttpResponseRedirect('http://127.0.0.1:8000/contact/%d' %
contact_id)

        else:
                f = EmailForm(initial={'contacts': contact.id})

        return render_to_response('addcontactemail.html', {'formset': f})

But I am getting "Caught an exception while rendering: 'long' object
is not iterable" thrown when the browser tries to render the form in
the addcontactemail.html template.

I can set other initial data, for example to the 'notes' CharField
fine.  I guess my question boils down to: how do you set initial data
for a many-to-many field?

Any pointers much appreciated!

Thanks,

Tom

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