2014-04-24 74 views
0

以下代碼成功地將一個新的ToolCalibration添加到我的數據庫,但它不保存SerialFormset或PartFormset。我一直在盯着這段代碼試圖找出相當長的一段時間,所以任何和所有的幫助將不勝感激。謝謝!保存Django Formsets

Forms.py

from django.forms import ModelForm 
from django.forms.models import inlineformset_factory 
from tool_cal.models import ToolCalibration, SerialNumber, PartNumber 

class ToolForm(ModelForm): 
    class Meta: 
     model = ToolCalibration 

SerialFormSet = inlineformset_factory(ToolCalibration, SerialNumber, can_delete=True) 
PartFormSet = inlineformset_factory(ToolCalibration, PartNumber, can_delete=True) 

Views.py

class ToolCreate(CreateView): 
model = ToolCalibration 
template_name = "create.html" 
form_class = ToolForm 
success_url = '/toolcal/success' 

def get(self, request, *args, **kwargs): 
    """ 
    Handles GET requests and instantiates blank versions of the form 
    and its inline formsets. 
    """ 
    self.object = None 
    form_class = self.get_form_class() 
    form = self.get_form(form_class) 
    serial_form = SerialFormSet(prefix='serial') 
    part_form = PartFormSet(prefix='part') 
    return self.render_to_response(
     self.get_context_data(form=form, 
           serial_form=serial_form, 
           part_form=part_form)) 

def post(self, request, *args, **kwargs): 
    """ 
    Handles POST requests, instantiating a form instance and its inline 
    formsets with the passed POST variables and then checking them for 
    validity. 
    """ 
    self.object = None 
    form_class = self.get_form_class() 
    form = self.get_form(form_class) 
    serial_form = SerialFormSet(self.request.POST, prefix='serial') 
    part_form = PartFormSet(self.request.POST, prefix='part') 
    if (form.is_valid() and serial_form.is_valid() and 
     part_form.is_valid()): 
     return self.form_valid(form, serial_form, part_form) 
    else: 
     return self.form_invalid(form, serial_form, part_form) 

def form_valid(self, form, serial_form, part_form): 
    """ 
    Called if all forms are valid. Creates a ToolCalibration instance along with 
    associated Serial and Parts and then redirects to a 
    success page. 
    """ 
    self.object = form.save() 
    serial_form.instance = self.object 
    serial_form.save() 
    part_form.instance = self.object 
    part_form.save() 
    return HttpResponseRedirect(self.get_success_url()) 

def form_invalid(self, form, serial_form, part_form): 
    """ 
    Called if a form is invalid. Re-renders the context data with the 
    data-filled forms and errors. 
    """ 
    return self.render_to_response(
     self.get_context_data(form=form, 
           serial_form=serial_form, 
           part_form=part_form)) 

回答

1

你有沒有考慮過使用django-extra-views?它包含一個用於處理InlineFormSets的快速簡單的CBV。

除了InlineFormSetViewGenericInlineFormSetView,他們也有CreateWithInlinesView,這似乎正是你想要的。相關文檔here

例子:

from extra_views import InlineFormSet, CreateWithInlinesView, UpdateWithInlinesView, 
from extra_views.generic import GenericInlineFormSet 

from tool_cal.models import ToolCalibration, SerialNumber, PartNumber  


class SerialNumberInline(InlineFormSet): 
    model = SerialNumber 


class PartNumberInline(GenericInlineFormSet): 
    model = PartNumber 


class ToolCreateView(CreateWithInlinesView): 
    model = ToolCalibration 
    inlines = [SerialNumberInline, PartNumberInline] 

    def get_success_url(self): 
     return self.object.get_absolute_url() 

即使你不想使用它們,你可以通過代碼挖掘,看看他們是如何處理它。

+0

謝謝!這絕對是一個有效的解決方案。 – Greg