如果我尝试通过CBV CreateView输入数据,则获取IntegrityError,但管理面板上的所有内容都可以正常工作。
型号:
class Account(Info):
firm = models.ForeignKey(
Firm
)
name = models.CharField(
max_length=100
)
is_default = models.BooleanField(
default=False
)
type = models.CharField(
max_length=1, choices=ACCOUNT_TYPE_CHOICES
)
category = models.CharField(
max_length=1, choices=ACCOUNT_CATEGORIES
)
sub_category = models.CharField(
max_length=1, choices=ACCOUNT_SUB_CATEGORIES
)
show_in = models.CharField(
max_length=1, choices=SHOW_IN
)
class Meta:
ordering = [
'name'
]
unique_together = (
'firm', 'name'
)
查看:
class CreateAccountView(LoginRequiredMixin, CreateView):
success_url = '/'
form_class = CreateAccountForm
http_method_names = ['get', 'post']
template_name = 'create_templates/create_account.html'
def get_context_data(self, **kwargs):
context = super(CreateAccountView, self).get_context_data(**kwargs)
firm_obj = self.get_firm_obj(self.kwargs.get('f_id'))
context['firm_obj'] = firm_obj
return context
def form_valid(self, form):
form.instance.firm = self.get_firm_obj(self.kwargs.get('f_id'))
form.instance.is_default = False
form.instance.sub_category = '4'
super(CreateAccountView, self).form_valid(form)
return HttpResponse(
'''
<script>
window.close();
window.onunload = refreshParent; function refreshParent() {
window.opener.location.reload();
}
</script>
'''
)
@staticmethod
def get_firm_obj(f_id):
return get_object_or_404(Firm, id=f_id)
形式:
class CreateAccountForm(forms.ModelForm):
name = forms.CharField(
label='Account Name', max_length=100,
widget=forms.TextInput(
attrs={
'class': 'form-control'
}
)
)
class Meta:
model = Account
fields = (
'name', 'type',
'category', 'show_in'
)
widgets = {
'type': forms.Select(
attrs={
'class': 'form-control'
}
),
'category': forms.Select(
attrs={
'class': 'form-control'
}
),
'show_in': forms.Select(
attrs={
'class': 'form-control'
}
)
}
回溯:
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\utils\decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\utils\decorators.py" in bound_func
25. return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Users\PrImE\workspace\kaizentech\emuneem\emuneem\girvi\utils.py" in dispatch
17. return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\views\generic\base.py" in dispatch
87. return handler(request, *args, **kwargs)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\views\generic\edit.py" in post
207. return super(BaseCreateView, self).post(request, *args, **kwargs)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\views\generic\edit.py" in post
173. return self.form_valid(form)
File "C:\Users\PrImE\workspace\kaizentech\emuneem\emuneem\muneem\views\create_views\create_account_view.py" in form_valid
25. super(CreateAccountView, self).form_valid(form)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\views\generic\edit.py" in form_valid
149. self.object = form.save()
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\forms\models.py" in save
457. construct=False)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\forms\models.py" in save_instance
103. instance.save()
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\db\models\base.py" in save
590. force_update=force_update, update_fields=update_fields)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\db\models\base.py" in save_base
618. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\db\models\base.py" in _save_table
699. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\db\models\base.py" in _do_insert
732. using=using, raw=raw)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\db\models\manager.py" in manager_method
92. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\db\models\query.py" in _insert
921. return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
920. cursor.execute(sql, params)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\db\backends\utils.py" in execute
81. return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\db\backends\utils.py" in execute
65. return self.cursor.execute(sql, params)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\db\utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\db\backends\utils.py" in execute
65. return self.cursor.execute(sql, params)
File "C:\Users\PrImE\envs\kaizentech\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
485. return Database.Cursor.execute(self, query, params)
Exception Type: IntegrityError at /add_account/f/1/
Exception Value: columns firm_id, name are not unique
使用sqlite
进行开发。如果我尝试通过管理面板输入重复条目,那么一切工作正常通过管理面板我得到Account with this Firm and Name already exists.
这是正确的,但为什么我没有通过CBV看到此错误
答案 0 :(得分:1)
您已从表单中排除firm
,并且在验证之前不会将其添加,因此表单无法验证公司和名称的组合是否为独特。您需要以其他方式在表单中传递它,并在表单clean
方法中明确检查该组合中没有现有实例。
答案 1 :(得分:0)
此定义表示&#34;此公司和名称的帐户已存在。&#34;在这两种情况下,您都收到相同的错误,
unique_together = (
'firm', 'name'
)
异常值:列firm_id,名称不唯一,这意味着与管理面板错误相同。