简体   繁体   English

如何在 django 中插入 ManyToMany 字段

[英]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.我想使用 django 在我的数据库中插入 ManyToMany 字段。我 select 一些客户使用复选框。 This is my models.py:这是我的模型.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:我在我的 views.py 中尝试了下面的代码,但没有用:

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.如果我删除 tergets 的一部分,它就可以工作。 But with this part it gives me This error: 'QuerySet' object has no attribute 'campaign' How I can solve this?但是对于这一部分,它给了我这个错误: 'QuerySet' object has no attribute 'campaign'我该如何解决这个问题?

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') .我怀疑它会打印None因为您排除了PartnerClient.objects.values('id')中的所有id On another note you don't need the all() since exclude() will return all the results you are looking for.另一方面,您不需要all()因为exclude()将返回您要查找的所有结果。

Two
In the line for target in targets: what exactly are you iterating through?for target in targets:你到底在迭代什么? targets = request.POST['targets'] is just giving you a string, so it would iterate through each letter. targets = request.POST['targets']只是给你一个字符串,所以它会遍历每个字母。 Perhaps you meant:也许你的意思是:

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

like you did for descriptions ?就像你对descriptions所做的那样? Or perhaps you are getting a list of items from your form, in which case you can use:或者您可能正在从表单中获取项目列表,在这种情况下您可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM