2011-06-18 27 views
2

我對Django相當新(實際上是4個月),在過去的2天裏我一直在爲這個問題苦苦掙扎,我想我犯了一些愚蠢的錯誤。任何幫助或輸入是高度讚賞。我正在使用Django 1.3。CheckboxSelectMultiple和ManyToManyField帶有Modelform的問題

在模型中我有,

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. 

我發現source of error here in Django model source

但尚不清楚如何解釋和鍛鍊了這個問題。

編輯: 我試圖使場空=真和/或空白=真還是我得到驗證錯誤,爲什麼呢?

隨着整個安裝我得到這個新的錯誤一些變化,

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, *args, **kwargs): 
    super(BusinessForm, self).__init__(*args, **kwargs) 
    self.fields['business_group_choices'].widget = forms.CheckboxSelectMultiple(choices=BUSINESS_GROUP) 

回答

3

有幾件事我會改變...

首先,我儘量避免使用「魔術弦」。相反的:

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, 'Multinational'), 
    (INT, 'International (Export/Import)'), 
    (DOM, 'Domestic/National'), 
    (LOC, 'Local'), 
    (VIR, 'Virtual'), 
) 

這就需要在模型中的變化,我已經改變了字段名稱是一個更清晰一點:

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的實例與您的業務相關聯。

希望能幫助你。

+0

感謝布蘭登,我真的很感激輸入。 –

+0

不客氣。 – Brandon

+0

我認爲你的解決方案非常好,我在2天后意識到,最終混合了你的一些想法。很好的學習,但再次感謝 –

0

好,我這樣做是正確, 第一件事,第一,切勿混用多對多和選擇,所以我的第2次嘗試是完全錯誤的。問題是在形式上,

所以現在最終解決方案的模樣,

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) 

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) 
相關問題