У меня есть дилемма, у меня есть форма, где необходимо ввести IDENTITY DOCUMENT, но я хочу, чтобы, когда работник компании заполнял это поле и затем отправлял форму, форма выдает сообщение о том, что Клиент существует , потому что IDENTITY DOCUMENT зарегистрирован в системе. Я использую GenericViews.
Форма
класс CustomerForm (forms.ModelForm):
class Meta:
model = CustomerData
fields = [
'name',
'last_name',
'dni',
'phone_number',
'address',
'age',
'birth_date',
'civil_status',
]
labels = {
'name': 'Nombre',
'last_name': 'Apellidos',
'dni': 'DNI',
'phone_number': 'Telefono',
'address': 'Direccion',
'age': 'Edad',
'birth_date': 'Fecha de Nacimiento',
'civil_status': 'Estado Civil',
}
widgets = {
'name': forms.TextInput(attrs={'class' : 'form-control'}),
'last_name':forms.TextInput(attrs={'class' : 'form-control'}),
'dni':forms.TextInput(attrs={'class' : 'form-control'}),
'phone_number':forms.TextInput(attrs={'class' : 'form-control'}),
'address':forms.Textarea(attrs={'class' : 'form-control'}),
'age':forms.NumberInput(attrs={'class' : 'form-control'}),
'birth_date':forms.DateInput(attrs={'class' : 'form-control'}),
'civil_status':forms.TextInput(attrs={'class' : 'form-control'}),
}
Модель
Класс CustomerData (models.Model):
«»»
Модель профиля.
Proxy model that extend the base data
with other informarion.
"""
name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
dni = models.CharField(max_length=8)
phone_number = models.CharField(max_length=20, blank=True)
address = models.TextField()
age = models.PositiveIntegerField(default=18)
birth_date = models.DateField()
civil_status = models.CharField(max_length=1)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = "Customer Data"
def __str__(self):
""" Return username """
return self.name + " " + self.last_name
View
class RegisterNewCustomer(CreateView):
model = CustomerData
form_class = CustomerForm
template_name = 'customers/customers_form.html'
success_url = reverse_lazy('customers:customers_list')
class CustomersList(ListView):
model=CustomerData
template_name = 'customers/customers_list.html'
class CustomerUpdate(UpdateView):
model = CustomerData
form_class = CustomerForm
template_name = 'customers/customers_form.html'
success_url = reverse_lazy('customers:customers_list')
class CustomerDelete(DeleteView):
model = CustomerData
template_name = 'customers/customers_delete.html'
success_url = reverse_lazy('customers:customers_list')
Template
{% extends 'base.html' %}
{% block content %}
<form method = "post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit"> Guardar </button>
</form>
{% endblock %}