0

我基本上是在构建功能来使用我的models.py中一个表中的值更新表单,表单将使用该表(潜在客户)填充初始值,并且在提交信息后,表单将填充另一个模型(潜在客户)

这是我的models.py

class Leads(models.Model):

    project_id = models.BigAutoField(primary_key=True, serialize=False)
    created_at = models.DateTimeField(auto_now_add=True)
    expected_revenue = MoneyField(decimal_places=2,max_digits=14, default_currency='USD')
    expected_licenses = models.IntegerField(blank=True)
    country = CountryField(blank_label='(select_country)')
    status = models.CharField(choices=[('Open', 'Open'), ('Closed', 'Closed'), ('Canceled', 'Canceled')], max_length=10)
    estimated_closing_date = models.DateField(blank=True)
    services = models.CharField(choices=[('Illumination Studies', 'Illumination Studies'),
                                  ('Training', 'Training'),('Survey Design Consultancy', 'Survey Design Consultancy'),
                                  ('Software License', 'Software License'),
                                  ('Software Development','Software Development')], max_length=40)
    agent = models.ForeignKey(Profile, default='agent',on_delete=models.CASCADE)
    company = models.ForeignKey(Company,on_delete=models.CASCADE)
    point_of_contact = models.ForeignKey(Client, default='agent',on_delete=models.CASCADE)
    updated_at = models.DateTimeField(auto_now=True)

class Deal(models.Model):
    project_id = models.ForeignKey(Leads, on_delete=models.CASCADE, default='id')
    agent = models.ForeignKey(Profile, on_delete=models.CASCADE, default="agent")
    service = models.ForeignKey(Leads, on_delete=models.CASCADE, related_name='service')
    closing_date = models.DateField(auto_now_add=True)
    client = models.ForeignKey(Client, on_delete=models.CASCADE,default='client')
    licenses = models.ForeignKey(Leads,on_delete=models.CASCADE, related_name='license')
    revenue = MoneyField(max_digits=14, decimal_places=2, default_currency='USD')
    comments = models.TextField(blank=True,null=True)
    company = models.ForeignKey(Company, on_delete=models.CASCADE)



表格.py

class NewDealForm(forms.ModelForm):
    class Meta:
        model = Deal
        fields = ['project_id','agent','client','company','service', 'licenses','revenue','comments']


@login_required
def close_lead(request):
    if request.method == 'POST':

        deal_form = NewDealForm(request.POST)
        print(deal_form)
        if deal_form.is_valid():
            deal_form.save()
            messages.success(request, 'You have successfully updated the status from open to Close')
            id = request.GET.get('project_id', '')
            obj = Leads.objects.get(project_id=id)
            obj.status = "Closed"
            obj.save(update_fields=['status'])

            return HttpResponseRedirect(reverse('dashboard'))
        else:

            messages.error(request, 'Error updating your Form')
    else:
        id = request.GET.get('project_id', '')
        obj = get_object_or_404(Leads, project_id=id)

        m = obj.__dict__
        keys = Leads.objects.get(project_id=m['project_id'])

        form_dict = {'project_id': keys.project_id,
                     'agent': keys.agent,
                     'client': keys.point_of_contact,
                     'company': keys.company,
                     'service': keys.services,
                     'licenses':keys.expected_licenses
                     }
        print(form_dict)
        form = NewDealForm(request.POST or None, initial=form_dict)

    return render(request,
                  "account/close_lead.html",
                  {'form': form})

我的问题是为什么除了服务字段之外的所有值都被填充?

未填充服务字段

有谁知道,我的字典的输出看起来是正确的,它包含服务信息,但不知何故这没有传递给表单?

{'project_id': 1, 'agent': <Profile: fcolina>, 'client': <Client: Zack M.>, 'company': <Company: E3>, 'service': 'Software License', 'licenses ':3}

如您所见,服务具有实际价值

4

1 回答 1

0

在您的Deal模型中,该services字段是一个外键Leads

class Deal(models.Model):
    service = models.ForeignKey(Leads)

然后您尝试使用servicesin 字段的值填充它,那Leads是一个字符串。

class Leads(models.Model):
    services = models.CharField()

查看您的模型,您需要使用实例填充该services 字段:Leads


form_dict = {'project_id': keys.project_id,
                     'agent': keys.agent,
                     'client': keys.point_of_contact,
                     'company': keys.company,
                     'service': keys, # <-- point to Leads
                     'licenses':keys.expected_licenses
                     }
于 2021-11-16T17:56:55.030 回答