1
我的應用程序出現問題。 將Django升級到最新版本 - 1.11.4後,我的三個測試中出現同樣的錯誤。將Django升級到1.11 - formset變更
AssertionError: False is not true : The formset 'formset' in context 0 does not contain the non-form error 'Musi być podana co najmniej jedna stawka' (actual errors: ['Proszę wysłać 1 lub więcej formularzy.'])
我知道錯誤是在波蘭的語言,但其實是一個,它使用Django 1.9和1.10的Django工作,但它不是Django的1.11。
有人能告訴我在Django 1.11中有什麼變化嗎? 我已經閱讀了很多關於它的東西,我已經嘗試了幾乎所有東西,但舊版本無法正常工作。
也許這個formset沒有看到我的錯誤信息,我必須在升級後以另一種方式犯錯誤?
class BaseTaxRateInlineFormSet(UniqueFieldsFormSetMixin,
NotEmptyInlineFormSetMixin,
BaseInlineFormSet):
_unique_fields = ('valid_from',)
msg_at_least_one_required = __('Musi być podana co najmniej jedna stawka')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.queryset = self.queryset.order_by('valid_from')
超類:
class NotEmptyInlineFormSetMixin(object):
"""
Prevents from deleting last not empty row (when other are empty)
"""
msg_at_least_one_required = __(
'Co najmniej jeden wiersz musi być wypełniony'
)
code_at_least_one_required = 'at_least_one_required'
def _form_is_empty(self, form):
return not len(list(filter(None, form.cleaned_data.values())))
def clean(self):
super().clean()
if any(self.errors):
return
forms_to_delete = 0
forms_empty = 0
for form in self.forms:
if self._should_delete_form(form):
forms_to_delete += 1
elif self._form_is_empty(form):
forms_empty += 1
if forms_to_delete + forms_empty == self.total_form_count():
raise ValidationError(
self.msg_at_least_one_required,
code=self.code_at_least_one_required
)
class UniqueFieldsFormSetMixin(object):
"""
Checks if fields (_unique_fields) have unique values in all forms
"""
_unique_fields = []
msg_field_not_unique = __('Wartość nie może się powtarzać')
code_field_not_unique = 'field_not_unique'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if 'unique_fields' in kwargs:
self._unique_fields = kwargs.pop('unique_fields', [])
def clean(self):
super().clean()
if self._unique_fields:
values = {field: set() for field in self._unique_fields}
for form in self.forms:
for field in self._unique_fields:
val = form.cleaned_data.get(field)
if val:
if val in values[field]:
form.add_error(
field,
ValidationError(
self.msg_field_not_unique,
code=self.code_field_not_unique
)
)
values[field].add(val)
和測試:
def test_update_removing_last_row(self):
data = {
# One row for existing rates and one empty.
self.FORMSET_PREFIX + '-TOTAL_FORMS': 2,
self.FORMSET_PREFIX + '-INITIAL_FORMS': 1,
self.FORMSET_PREFIX + '-MAX_NUM_FORMS': 100
}
# First row exists
data = self._load_formset_data(
data, dict(
self.rate_data_1, DELETE='on',
**{'id': self.rate_pk_1, self.related_field: self.instance_pk}
), name=self.instance_name
)
self.client.force_login(self.superuser)
response = self.client.post(self.url_update, data=data)
# Can't remove last row
self.assertEqual(response.status_code, 200)
self.assertFormsetError(
response, 'formset', None, None,
self.base_formset_class.msg_at_least_one_required
)