每当用户尝试创建具有相同名称的对象时,我想显示错误。我希望公司名称是独一无二的。 Tried但问题仍然存在。 Django应该在表单中显示错误,但为什么我会得到这个。????
Firm model:
class Firm(models.Model):
name = models.TextField(
verbose_name='firm name',
max_length=100, unique=True
)
address = models.TextField(
max_length=100
)
phone_no_1 = models.CharField(
max_length=10, null=True, blank=True
)
phone_no_2 = models.CharField(
max_length=10, null=True, blank=True
)
reg_no = models.TextField(
max_length=25, null=True, blank=True
)
但相反,我得到了这个。
Traceback:
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\egirvi\dev\egirvi\girvi\utils.py" in dispatch
14. 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\egirvi\dev\egirvi\girvi\views\create_views\create_firm_view.py" in form_valid
16. super(CreateFirmView, 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\workspace\kaizentech\egirvi\dev\egirvi\girvi\models\firm.py" in save
29. return super(Firm, self).save(*args, **kwargs)
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_firm/
Exception Value: column name is not unique
查看:
class CreateFirmView(LoginRequiredMixin, CreateView):
form_class = CreateFirmForm
http_method_names = ['get', 'post']
success_url = reverse_lazy('girvi:find_customer')
template_name = 'create_templates/create_firm.html'
def form_valid(self, form):
from django.shortcuts import HttpResponse
super(CreateFirmView, self).form_valid(form)
return HttpResponse(
'''
<script>
window.close();
window.onunload = refreshParent; function refreshParent() {
window.opener.location.reload();
}
</script>
'''
)
形式:
class CreateFirmForm(forms.ModelForm):
from girvi.utils import validators
phone_no_1 = forms.CharField(
label='Phone No. 1', required=False, validators=[
validators.get('only_numbers'), validators.get('min_value')(10)
],
widget=forms.TextInput(
attrs={'placeholder': 'Mobile Number', 'class': 'form-control'}
)
)
phone_no_2 = forms.CharField(
label='Phone No. 2 ', required=False, validators=[
validators.get('only_numbers'), validators.get('max_value')(10), validators.get('max_value')(10)
],
widget=forms.TextInput(
attrs={'placeholder': 'Mobile Number', 'class': 'form-control'}
)
)
class Meta:
model = Firm
fields = '__all__'
widgets = {
'name': forms.TextInput(
attrs={'autofocus': 'true', 'class': 'form-control', 'placeholder': 'Firm Name'}
),
'address': forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'Address'}
),
'reg_no': forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'Reg No.'}
)
}
SQL:
CREATE TABLE "girvi_firm" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(100) NOT NULL UNIQUE, "address" text NOT NULL, "phone_no_1" varchar(10) NULL, "phone_no_2" varchar(10) NULL, "reg_no" text NULL)
答案 0 :(得分:1)
数据库抛出此错误。
但是django ModelForm会自动处理独特的成本计算。
不要忘记调用父表单的clean()
方法:
from django import forms
class FirmForm(forms.ModelForm):
class Meta:
model = Firm
def clean(self):
# do some your stuff here
# don't forget to call super().clean
return super(FirmForm, self).clean()
用django docs写的:
ModelForm.clean()方法设置一个标志,使模型验证步骤验证标记为unique,unique_together或unique_for_date | month | year的模型字段的唯一性。
如果要覆盖clean()方法并维护此验证,则必须调用父类的clean()方法。