2016-03-15 53 views
2

我想在我的應用程序中創建帳單。我有一個extra_bill_form和multible item_forms。Formset對象沒有屬性'absolute_max'

觀點:

def ExtraBillView(request): 
    item_form_set = formset_factory(ItemForm, formset=BaseItemFormSet, can_delete=True) 
    extra_bill_form = ExtraBillForm() 

    if request.method == 'POST': 
     item_form_set = BaseItemFormSet(request.POST) 
     extra_bill_form = ExtraBillForm(request.POST) 

     if extra_bill_form.is_valid() and item_form_set.is_valid(): 
      al1 = extra_bill_form.cleaned_data.get('adress_line1') 
      al2 = extra_bill_form.cleaned_data.get('adress_line2') 
      al3 = extra_bill_form.cleaned_data.get('adress_line3') 
      al4 = extra_bill_form.cleaned_data.get('adress_line4') 

      for form in item_form_set: 
       pass 
     pdb.set_trace() 

     else: 
      extra_bill_form = ExtraBillForm() 
      item_form_set = formset_factory(ItemForm, formset=BaseItemFormSet, extra=0, max_num=5, min_num=1) 

    context = { 
     'extra_bill_form': extra_bill_form, 
     'item_form_set': item_form_set, 
    } 

    return render(request, 'extra_bill.html', context) 

形式:

class ExtraBillForm(forms.Form): 
    adress_line1 = forms.CharField(label='Adresszeile 1:', required=True) 
    adress_line2 = forms.CharField(label='Adresszeile 2:', required=False) 
    adress_line3 = forms.CharField(label='Adresszeile 3:', required=False) 
    adress_line4 = forms.CharField(label='Adresszeile 4:', required=False) 


class ItemForm(forms.Form): 
    name = forms.CharField(label='Artikelname:') 
    ust = forms.IntegerField(label='UST:', initial=20) 
    price_nto = forms.IntegerField(label='Preis netto:', required=False) 
    price_brt = forms.IntegerField(label='Preis brutto:', required=False) 


class BaseItemFormSet(BaseFormSet): 
    def clean(self): 
     if self.price_nto is None: 
      if self.price_brt is None: 
       return 
     if any(self.errors): 
      return 

如果我submitt我的形式,我得到一個錯誤:

'BaseItemFormSet' object has no attribute 'absolute_max' how to solve this problem?

Environment: 
Request Method: POST 
Request URL: http://127.0.0.1:8000/calculator/extrabill/ 

Django Version: 1.9.4 
Python Version: 3.5.1 
Installed Applications: 
['django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'django_ajax', 
'django_bootstrap_dynamic_formsets', 
'Bank', 
'bootstrap3', 
'Building', 
'Calculator', 
'CounterChange', 
'Customer', 
'Credit', 
'Hackgut', 
'Heatingplant', 
'Index', 
'jquery', 
'jquery_ui', 
'Measurement', 
'Price', 
'Rate'] 
Installed Middleware: 
['django.middleware.security.SecurityMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.common.CommonMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'django.middleware.clickjacking.XFrameOptionsMiddleware'] 

Traceback: 

File "/usr/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response 
    149. response = self.process_exception_by_middleware(e, request) 

File "/usr/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response 
    147. response = wrapped_callback(request, *callback_args, **callback_kwargs) 

File "/home/klaus/PycharmProjects/waerme/Calculator/views.py" in ExtraBillView 
    94. if extra_bill_form.is_valid() and item_form_set.is_valid(): 

File "/usr/lib/python3.5/site-packages/django/forms/formsets.py" in is_valid 
    316. self.errors 

File "/usr/lib/python3.5/site-packages/django/forms/formsets.py" in errors 
    290. self.full_clean() 

File "/usr/lib/python3.5/site-packages/django/forms/formsets.py" in full_clean 
    337. for i in range(0, self.total_form_count()): 

File "/usr/lib/python3.5/site-packages/django/forms/formsets.py" in total_form_count 
    116. return min(self.management_form.cleaned_data[TOTAL_FORM_COUNT], self.absolute_max) 

Exception Type: AttributeError at /calculator/extrabill/ 
Exception Value: 'BaseItemFormSet' object has no attribute 'absolute_max' 
+0

請發表完整的追溯。 – koniiiik

+0

這裏的完整回溯http://dpaste.com/17NGSX6 – spitzbuaamy

+0

而不是鏈接到pastebins,你可以將它作爲一大塊代碼添加到問題?此外,這不是追溯,而是一段周邊代碼和INSTALLED_APPS和中間件列表。 – koniiiik

回答

4

該formset工廠創建表單集類。您只能撥打formset_factory一次。您可以在視圖外進行此操作。

ItemFormSet = formset_factory(ItemForm, formset=BaseItemFormSet, extra=0, max_num=5, min_num=1) 

然後,在你看來,實例化是由formset_factory創建ItemFormSet。你不應該實例化BaseItemFormSet

if request.method == 'POST': 
    item_form_set = ItemFormSet(request.POST) 
    ... 
else: 
    item_form_set = ItemFormSet() # blank formset for GET request 

context = { 
    'extra_bill_form': extra_bill_form, 
    'item_form_set': item_form_set, 
} 

return render(request, 'extra_bill.html', context)