Hello there, I'm trying to do a form that can store the customers records, but recently it's throw me this error
TypeError at /crear-cliente/
object of type 'int' has no len()
C:\Users\G-FIVE\Desktop\practicas y proyectos\mini_erp\MiniErp\customers\views.py
, line 43, in register_customer
-
address = address
-
)
-
-
customer.save()
-
"""
-
form = FormCustomer(request.POST)
-
-
if form.is_valid():
…
-
data_form = form.cleaned_data
-
-
first_name = data_form['first_name']
-
last_name = data_form['last_name']
-
cedula = data_form['cedula']
-
phone_number = data_form['phone_number']
I did the form using the Django classes, I'll show you the code of the form, the view and the model
Form code:
from django import forms
from django.core import validators
# Clase para el formulario de registro de clientes
class FormCustomer(forms.Form):
# Input para el first_name
first_name = forms.CharField(
label='Nombre',
required=True,
widget=forms.TextInput(
attrs={
'placeholder':'Ej: John',
'class': 'form-control form-control-user mb-3',
'id': 'first_name'
}
),
validators= [
validators.MinLengthValidator(3, 'El nombre está incompleto')
]
)
# Input para el last_name
last_name = forms.CharField(
label='Apellido',
required=True,
widget=forms.TextInput(
attrs={
'placeholder':'Ej: Doe',
'class': 'form-control form-control-user mb-3',
'id': 'last_name'
}
),
validators= [
validators.MinLengthValidator(3, 'El nombre está incompleto')
]
)
# Input para la cédula
cedula = forms.IntegerField(
label='Cédula',
required=True,
widget=forms.NumberInput(
attrs={
'placeholder':'xx xxx xxx',
'class': 'form-control form-control-user mb-3',
'id': 'cedula'
}
),
validators= [
validators.MinLengthValidator(7, 'Faltan números'),
validators.MaxLengthValidator(8, 'Has introducido más números')
]
)
# Input para el teléfono
phone_number = forms.IntegerField(
label='Teléfono',
required=True,
widget=forms.NumberInput(
attrs={
'placeholder':'04xx xxx xx xx',
'class': 'form-control form-control-user mb-3',
'id': 'phone_number'
}
),
validators= [
validators.MinLengthValidator(11, 'Faltan números'),
validators.MaxLengthValidator(11, 'Has introducido más números')
]
)
# Input para la dirección
address = forms.CharField(
label='Dirección',
widget=forms.Textarea(
attrs={
'class': 'form-control form-control-user mb-3',
'style': 'min-height: 150px; max-height: 150px;',
'id': 'address'
}
)
)
View code:
# Vista que se encarga de registrar al cliente (C)
def register_customer(request):
if request.method == 'POST':
form = FormCustomer(request.POST)
if form.is_valid():
data_form = form.cleaned_data
first_name = data_form['first_name']
last_name = data_form['last_name']
cedula = data_form['cedula']
phone_number = data_form['phone_number']
address = data_form['address']
customer = Customer(
first_name = first_name,
last_name = last_name,
cedula = cedula,
phone_number = phone_number,
address = address
)
customer.save()
return redirect('home')
elif request.method == 'GET':
return redirect('create_customer')
Model code:
# Clase para la tabla Cliente
class Customer(models.Model):
first_name = models.CharField(max_length=100, verbose_name='Nombre(s)')
last_name = models.CharField(max_length=100, verbose_name='Apellido(s)')
cedula = models.IntegerField(max_length=8, verbose_name='Cédula')
phone_number = models.IntegerField(max_length=11, verbose_name="Teléfono")
address = models.TextField(verbose_name='Dirección')
created_at = models.DateTimeField(auto_now_add=True, verbose_name="Creado el")
updated_at = models.DateTimeField(auto_now=True, verbose_name="Actualizado el")
class Meta:
verbose_name = 'Cliente'
verbose_name_plural = 'Clientes'
ordering = ['-created_at']
def __str__(self):
return self.first_name
I'd like that someone can give me a little help
I have tried to look for examples and I have seen code similar to mine
CodePudding user response:
You are using MinLengthValidator
and MaxLengthValidator
on the IntegerFields, which tries to use the len()
function on integers. This is not possible. Therefore, your cedula
and phone_number
fields will produce the object of type 'int' has no len()
error. You can either look into using MaxValueValidator
and MinValueValidator
with integer
for validators instead of MinLengthValidator
and MaxLengthValidator
or change the fields to CharField
.
I'm not sure what cedula
is, but I would recommend making phone_number
a string. Phone numbers are not considered integers and can have special characters, such as adding a area code.