问题描述
||
我对Django很陌生(实际上是4个月),最近两天我一直在为这个问题而苦苦挣扎,我想我犯了一些愚蠢的错误。任何帮助或投入,我们将不胜感激。我正在使用Django 1.3。
在我的模型中
BUSInesS_GROUP = (
(\'MNC\',\'Multinational\'),(\'INT\',\'International (Export/Import)\'),(\'DOM\',\'Domestic/National\'),(\'LOC\',\'Local\'),(\'VIR\',\'Virtual\'),)
class BusinessGroup(models.Model):
bgroup_type = models.CharField(max_length=15,choices = BUSInesS_GROUP,blank = True,null = True)
class Business(models.Model):
business_group_choices = models.ManyToManyField(BusinessGroup,verbose_name= \"Business Group\")
在表格中,我有类似的内容,
def __init__(self,*args,**kwargs):
super(BusinessForm,self).__init__(*args,**kwargs)
self.fields[\'business_group_choices\'].widget = forms.CheckBoxSelectMultiple(choices=BUSInesS_GROUP)
在视图中
if request.method == \'POST\':
form = BusinessForm(request.POST,instance = business)
if form.is_valid():
new_business = form.save(commit=False)
new_business.created_by = request.user
form_values = form.cleaned_data
new_business.save()
assign(\'edit_business\',request.user,new_business)
return HttpResponseRedirect(new_business.get_absolute_url())
我遇到类似的错误,
\"DOM\" is not a valid value for a primary key.
\"INT\" is not a valid value for a primary key.
等等
我在Django模型源中找到了错误源,
但是不清楚如何解释和锻炼这个问题。
编辑:
我尝试将字段设置为null = True和/或blank = True仍然出现验证错误,为什么?
在整个设置上进行了一些更改后,我得到了这个新错误,
Select a valid choice. [u\'MNC\',u\'INT\',u\'DOM\',u\'LOC\',u\'VIR\'] is not one of the available choices.
新设置:
在模型中
class BusinessGroup(models.Model):
bgroup_type = models.CharField(max_length=15)
class Business(models.Model):
business_group_choices = models.ManyToManyField(BusinessGroup,verbose_name= \"Business Group\",choices=BUSInesS_GROUP)
在表格中,我有类似的内容,
def __init__(self,**kwargs)
self.fields[\'business_group_choices\'].widget = forms.CheckBoxSelectMultiple(choices=BUSInesS_GROUP)
解决方法
有几件事我会改变的...
首先,我会尽量避免使用“魔术字符串”。代替:
BUSINESS_GROUP = (
(\'MNC\',\'Multinational\'),(\'INT\',\'International (Export/Import)\'),(\'DOM\',\'Domestic/National\'),(\'LOC\',\'Local\'),(\'VIR\',\'Virtual\'),)
我会做:
#in my_app > constants.py
MNC = 0
INT = 1
DOM = 2
LOC = 3
VIR = 4
BUSINESS_GROUP_CHOICES = (
(MNC,(INT,(DOM,(LOC,(VIR,)
这需要更改您的模型,并且我已经更改了字段名称,使其更加清晰:
from django.db import models
from businesses.constants import BUSINESS_GROUP_CHOICES,MNC
class BusinessGroup(models.Model):
business_group_type = models.IntegerField(choices=BUSINESS_GROUP_CHOICES,default=MNC)
def __unicode__(self):
choices_dict = dict(BUSINESS_GROUP_CHOICES)
return choices_dict[self.business_group_type]
class Business(models.Model):
business_groups = models.ManyToManyField(BusinessGroup)
真正的问题在于您的形式。您不仅需要重新定义窗口小部件,还需要使用ModelMultipleChoiceField,如下所示:
from django import forms
from businesses.models import BusinessGroup
class BusinessForm(forms.ModelForm):
class Meta:
model = Business
business_group = forms.ModelMultipleChoiceField(queryset=BusinessGroup.objects.all(),widget=forms.CheckboxSelectMultiple())
这就是为什么您遇到主键错误的原因。您的BUSINESS_GROUP只是一个元组。 Django试图将您选择的元组中的值分配为主键,这显然是行不通的。相反,ModelMultipleChoiceField将要执行的操作是将所选BusinessGroup的实例与业务相关联。
希望对您有所帮助。
, 好吧,我说对了,
首先,永远不要将ManyToMany和选择混在一起,所以我的第二次尝试是完全错误的。问题出在形式上
所以现在最终的解决方案看起来像
BUSINESS_GROUP = (
(\'MNC\',)
class BusinessGroup(models.Model):
bgroup_type = models.CharField(max_length=15)
class Business(models.Model):
business_group_choices = models.ManyToManyField(BusinessGroup)
而不是,
class BusinessForm(forms.ModelForm):
def __init__(self,*args,**kwargs):
super(BusinessForm,self).__init__(*args,**kwargs)
self.fields[\'business_group_choices\'].widget = forms.CheckboxSelectMultiple(choices=BUSINESS_GROUP)
在表格中,我有类似的内容,
class BusinessForm(forms.ModelForm):
business_group_choices = forms.MultipleChoiceField(label=\"Business Group\",widget=forms.CheckboxSelectMultiple,choices=BUSINESS_GROUP)
您需要将MultipleChoiceField与CheckboxSelectMultiple一起使用。
模型中的这个完全错误(混合M2M和选择),
class Business(models.Model):
business_group_choices = models.ManyToManyField(BusinessGroup,verbose_name= \"Business Group\",choices=BUSINESS_GROUP)