'How to insert ManyToMany field in django

I want to insert a ManyToMany fields in my db using django.I select some customers using checkboxes. This is my models.py :

class Campaign(models.Model):
    title = models.CharField(max_length=255)
    channel = models.CharField(max_length=255)
    start_date = models.DateField()
    end_date = models.DateField()
    target_prospect = models.ManyToManyField(ProspectClient,related_name='campaigns_prospect')
    target_partner = models.ManyToManyField(PartnerClient,related_name='campaigns_partners')

I try the code below in my views.py but didn't work :

def campaigns_page(request):
    if request.user.is_authenticated:
        if request.user.profile == 'D' or request.user.profile == 'E' or request.user.is_superuser:
            campaigns = Campaign.objects.all()
            prospects = ProspectClient.objects.all()
            partners = PartnerClient.objects.exclude(id__in=PartnerClient.objects.values('id')).all()
            context = {
                'campaigns':campaigns,
                'prospects':prospects,
                'partners':partners
            }
            if request.method == 'POST':
                title = request.POST['title']
                channel = request.POST['channel']
                start_date = request.POST['start_date']
                end_date = request.POST['end_date']
                descriptions = request.POST['goals'].split(",")
                targets = request.POST['targets']
                campaign = Campaign.objects.create(title=title,channel=channel,start_date=start_date,end_date=end_date)
                for description in descriptions:
                    goal = Goal.objects.create(description=description)
                    goal.campaign.add(campaign)
                for target in targets:
                    prospects.campaign.add(campaign)
                    partners.campaign.add(campaign)
            return render(request,'CampaignManagement/campaigns_page.html',context)
    return render(request, 'Login/logout.html')

If I delete the part of tergets it works. But with this part it gives me This error : 'QuerySet' object has no attribute 'campaign' How I can solve this ?



Solution 1:[1]

I see a couple of errors. Perhaps one or more are leading to the problem.

One
Try printing this:

partners = PartnerClient.objects.exclude(id__in=PartnerClient.objects.values('id')).all()
print(partners)

I suspect it will print None since you are excluding all id's in PartnerClient.objects.values('id'). On another note you don't need the all() since exclude() will return all the results you are looking for.

Two
In the line for target in targets: what exactly are you iterating through? targets = request.POST['targets'] is just giving you a string, so it would iterate through each letter. Perhaps you meant:

targets = request.POST['targets'].split(", ")

like you did for descriptions? Or perhaps you are getting a list of items from your form, in which case you can use:

targets = request.POST.getlist('targets')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 raphael