如何在视图中设置ForeignKey值

时间:2018-06-29 10:58:22

标签: python django python-3.x

我的用户注册表单包含一个ForeignKeyField,并且我正在尝试在视图中设置此ForeignKeyField的值。从下面的代码中可以看出,我向视图传递了一个参数“ pk”,该参数用于在呈现表单之前设置ForeignKeyField值,但这似乎不起作用。发布表单后,出现此错误“((1048,“ Column'package_id'不能为空”))。

这是我的观点。

def subscribe_view(request, billing_cycle, pk):
    if request.method == 'POST':
        userRegForm = CustomUserForm(request.POST)
        companyForm = CompanyProfileForm(request.POST)

        if userRegForm.is_valid() and companyForm.is_valid():
            user = userRegForm.save()
            compProfile = companyForm.save(commit=False)
            if compProfile.user_id is None:
                compProfile.user_id = user.id
            compProfile.save()

            selected_provinces = companyForm.cleaned_data['provinces']
            selected_cats = companyForm.cleaned_data['tenderCategory']
            selected_package = companyForm.cleaned_data['package']

            for province_item in selected_provinces:
                compProfile.provinces.add(province_item)

            for cat_item in selected_cats:
                compProfile.tenderCategory.add(cat_item)

            keyword_ids_str = companyForm.cleaned_data['keywordListItem']
            if keyword_ids_str is not '' or keyword_ids_str is not None:
                keyword_ids = keyword_ids_str.split(',')[:-1]
                for keyword_id in keyword_ids:
                    keywordObj = Keywords.objects.get(id=int(keyword_id.strip()))
                    compProfile.keywords.add(keywordObj)

            compProfile.package(selected_package)

            return HttpResponseRedirect('/user_account/dashboard')
        else:
            return HttpResponseRedirect(request.path)
    else:
        packageOption = Packages.objects.get(id=pk)

        if billing_cycle == '1' or billing_cycle == '0':
            b_cycle = billing_cycle
        else:
            b_cycle = '0'

        userRegForm = CustomUserForm()
        companyProfileForm = CompanyProfileForm(initial={'package': packageOption.id})
        bankingDetailsForm = BankingDetailsForm()

        #companyProfileForm.fields["package"].initial = packageOption.id

        args = {'userRegForm': userRegForm,
                'package': packageOption,
                'billing_cycle': b_cycle,
                'companyProfileForm': companyProfileForm,
                'bankingDetailsForm': bankingDetailsForm
        }
        args.update(csrf(request))
        return render(request, 'user_account/subscribe.html', args)

这是模特:

class CompanyProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    companyName = models.CharField(max_length=200, blank=False)
    companyRegNum = models.CharField(max_length=30, blank=True)
    contactNumber = models.CharField(max_length=20, blank=False)
    address = models.CharField(max_length=300, blank=True)
    areaCode = models.CharField(max_length=10, blank=False)
    deliveryEmails = models.TextField(blank=True)   #this is the list of all the people chosen to recieve daily notification.
    tenderCategory = models.ManyToManyField(category, blank=False)    #links the user to the chosen category.
    provinces = models.ManyToManyField(Province, blank=False)    #links the user to the chosen Provinces.
    package = models.ForeignKey(Packages, blank=False)       #links the user to the chosen package.
    pymntMethod = models.IntegerField(blank=True, default=3)   #this is the chosen payment method (e.g credit card=1, debit order=2 or direct debit=3)
    keywords = models.ManyToManyField(Keywords)     #links the user to the chosen keywords.
    extraKeywords = models.TextField(default='', blank=True)  #this field acts as a container of extra keywords from the user. These are keywords that we do not have in our database.
    termsAndConditions = models.BooleanField(blank=False, default=1)   #this is the T&C's field that must be agreed to by the client.
    commencementDate = models.DateTimeField(default=timezone.now, blank=True)

    def __str__(self):
        return self.companyName

这是我的表格。

class CompanyProfileForm(ModelForm):
    provinces = forms.ModelMultipleChoiceField(queryset=Province.objects.all(), widget=forms.SelectMultiple(attrs={
        'id': 'provinces'
    }))

    tenderCategory = forms.ModelMultipleChoiceField(queryset=category.objects.all(), widget=forms.SelectMultiple(attrs={
        'id': 'catSelect'
    }))

    keywordListItem = forms.CharField(widget=forms.HiddenInput(attrs={
        'id': 'keywordListItem_id'
    }))

    class Meta:
        model = CompanyProfile
        exclude = (
            'user',
            'keywords',
            'commencementDate',
            'package',
        )
        fields = (
            'companyName',
            'companyRegNum',
            'contactNumber',
            'address',
            'areaCode',
            'deliveryEmails',
            'provinces',
            'tenderCategory',
            'termsAndConditions',
        )

        widgets = {
            'companyName': forms.TextInput(attrs={
                            'class': 'form-control',
                            'id': 'companyNameId',
                            'name': 'companyName',
                            'required': 'required',
                            'data-rule-required': 'true',
                            'data-msg-required': 'Please enter company name'
                        }),
            'companyRegNum': forms.TextInput(attrs={
                            'class': 'form-control',
                            'id': 'companyRegNumId',
                            'name': 'companyRegNum',
                            'required': 'required',
                            'data-rule-required': 'true',
                            'data-msg-required': 'Please enter company reg number'
                        }),
            'contactNumber': forms.TextInput(attrs={
                            'class': 'form-control input_field',
                            'id':'contactNum',
                            'name': 'contactNumber',
                            'required': 'required',
                            'data-rule-required': 'true',
                            'data-msg-required': 'Please enter contact number'
                        }),
            'address': forms.TextInput(attrs={
                            'class': 'form-control',
                            'id': 'addressId',
                            'name': 'address',
                            'required': 'required',
                            'data-rule-required': 'true',
                            'data-msg-required': 'Please enter the address'
                        }),
            'areaCode': forms.TextInput(attrs={
                            'class': 'form-control',
                            'id': 'areaCodeId',
                            'name': 'areaCode',
                            'required': 'required',
                            'data-rule-required': 'true',
                            'data-msg-required': 'Please enter the areaCode'
                        }),
            'deliveryEmails': forms.TextInput(attrs={
                            'class': 'form-control textInput',
                            'id': 'deliveryEmailId',
                            'name': 'deliveryEmails',
                            'required': 'required',
                            'data-rule-required': 'true',
                            'data-msg-required': 'Please enter the email address(s)'
                        }),

            'package': forms.HiddenInput(),
            'pymntMethod': forms.HiddenInput(attrs={
                            'id': 'pymntType',
                            'value': '3'
                        }),
            'termsAndConditions': forms.CheckboxInput(attrs={
                            'id': 'termsAndConditions'
                        })
        }

有人知道如何解决此错误吗?

1 个答案:

答案 0 :(得分:0)

有些事情可能会助您一臂之力:

  1. package被列为ForeignKey字段,未设置null=True,这是必填字段。这样,在保存模型实例之前需要有一个值。设置default属性的原因解决了该错误,原因是您现在为此提供的值不是null。如果这是一个允许为空的字段,请更新模型以使其包含null=True,并确保进行迁移以更新数据库,然后重试。

  2. 在更新package时,我注意到要分配的包就像传递给方法一样被传递给它。我不知道这是有效的,因为属性通常是通过直接分配(compProfile.package = selected_package)来更新的。

  3. 在更新模型实例的属性(compProfileCompanyProfile的模型实例)时,对该实例的属性所做的任何更改都不会持久保存到数据库中,除非save()叫做。这意味着,如果稍后从数据库中获取它,则package将与分配的内容不匹配。确保在修改后调用compProfile.save()