2010-05-22 57 views
1

我有一個Django的'添加業務'視圖,它增加了一個新的業務,內嵌'business_contact'表單。測試Django Inline ModelForms:如何安排POST數據?

形式工作正常,但我不知道怎麼寫了單元測試 - 具體而言,「POSTDATA」發送給self.client.post(settings.BUSINESS_ADD_URL,POSTDATA)

我已經檢查我的瀏覽器中的字段,並嘗試添加具有相應名稱的發佈數據,但運行時仍出現'ManagementForm數據丟失或被篡改'錯誤。

任何人都知道如何發佈內嵌數據的任何資源?

相關型號,如果有幫助,查看&表格。洛塔謝謝。

MODEL:

class Contact(models.Model): 
    """ Contact details for the representatives of each business """ 
    first_name = models.CharField(max_length=200) 
    surname = models.CharField(max_length=200) 
    business = models.ForeignKey('Business') 
    slug = models.SlugField(max_length=150, unique=True, help_text=settings.SLUG_HELPER_TEXT) 
    created = models.DateTimeField(auto_now_add=True) 
    updated = models.DateTimeField(auto_now=True) 
    phone = models.CharField(max_length=100, null=True, blank=True) 
    mobile_phone = models.CharField(max_length=100, null=True, blank=True) 
    email = models.EmailField(null=True) 
    deleted = models.BooleanField(default=False) 
    class Meta: 
     db_table='business_contact' 

    def __unicode__(self): 
     return '%s %s' % (self.first_name, self.surname) 

    @models.permalink 
    def get_absolute_url(self): 
     return('business_contact',(), {'contact_slug': self.slug }) 

class Business(models.Model): 
    """ The business clients who you are selling products/services to """ 
    business = models.CharField(max_length=255, unique=True) 
    slug = models.SlugField(max_length=100, unique=True, help_text=settings.SLUG_HELPER_TEXT) 
    description = models.TextField(null=True, blank=True) 
    primary_contact = models.ForeignKey('Contact', null=True, blank=True, related_name='primary_contact') 
    business_type = models.ForeignKey('BusinessType') 
    deleted = models.BooleanField(default=False) 
    created = models.DateTimeField(auto_now_add=True) 
    updated = models.DateTimeField(auto_now=True) 
    address_1 = models.CharField(max_length=255, null=True, blank=True) 
    address_2 = models.CharField(max_length=255, null=True, blank=True) 
    suburb = models.CharField(max_length=255, null=True, blank=True) 
    city = models.CharField(max_length=255, null=True, blank=True) 
    state = models.CharField(max_length=255, null=True, blank=True) 
    country = models.CharField(max_length=255, null=True, blank=True) 
    phone = models.CharField(max_length=40, null=True, blank=True) 
    website = models.URLField(null=True, blank=True) 
    class Meta: 
     db_table = 'business' 

    def __unicode__(self): 
     return self.business 

    def get_absolute_url(self): 
     return '%s%s/' % (settings.BUSINESS_URL, self.slug) 

瀏覽次數:

def business_add(request): 
    template_name = 'business/business_add.html' 
    if request.method == 'POST': 
     form = AddBusinessForm(request.POST) 
     if form.is_valid(): 
      business = form.save(commit=False) 
      contact_formset = AddBusinessFormSet(request.POST, instance=business) 
      if contact_formset.is_valid(): 
       business.save() 
       contact_formset.save() 
       contact = Contact.objects.get(id=business.id) 
       business.primary_contact = contact 
       business.save() 
       #return HttpResponse(help(contact)) 
       #business.primary = contact.id 
       return HttpResponseRedirect(settings.BUSINESS_URL) 
     else: 
      contact_formset = AddBusinessFormSet(request.POST) 
    else: 
     form = AddBusinessForm() 
     contact_formset = AddBusinessFormSet(instance=Business()) 
    return render_to_response(
      template_name, 
      { 
      'form': form, 
      'contact_formset': contact_formset, 
      }, 
      context_instance=RequestContext(request) 
      ) 

形式:

class AddBusinessForm(ModelForm): 
    class Meta: 
     model = Business 
     exclude = ['deleted','primary_contact',] 

class ContactForm(ModelForm): 
    class Meta: 
     model = Contact 
     exclude = ['deleted',] 


AddBusinessFormSet = inlineformset_factory(Business, 
     Contact, 
     can_delete=False, 
     extra=1, 
     form=AddBusinessForm, 
     ) 

回答

2

的問題是你有沒有包含在您的數據管理形式。您需要包含form-TOTAL_FORMS(formset中的表單總數,默認值爲2),form-INITIAL_FORMS(formset中的初始表單數,默認值爲0)和form-MAX_NUM_FORMS(表單集中表單的最大數量,默認值爲'' )。

有關管理窗體的更多信息,請參閱Formset documentation

+0

非常感謝約翰 - 工作的一種享受。 – PlankTon 2010-05-22 09:54:11