视图中出现 TypeError:字段需要一个数字但得到的是“”
TypeError in view: Field expected a number but got ' '
这是我第一次在堆栈溢出中发布问题,所以请多多包涵 :D
我正在尝试从表单创建员工模型,但错误一直存在
这是我的模型文件:
class CustomUser(AbstractUser):
user_type_data = ((1, "HOD"), (2, "Consultant"))
user_type = models.CharField(default=1, choices=user_type_data, max_length=10)
class Consultant(models.Model):
CHOICES = (
('P', 'au paiement'),
('J15', 'J+15'),
('J30', 'J+30'),
)
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
client_id=models.ForeignKey(Client,on_delete=models.CASCADE,default=1)
tarif_facturation = models.IntegerField(blank=True, null=True)
tarif_Consultant = models.IntegerField(blank=True, null=True)
cnss = models.IntegerField(blank=True, null=True)
rib = models.IntegerField(blank=True, null=True)
paiement = models.CharField(max_length=300, choices=CHOICES)
profile_pic = models.FileField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
objects = models.Manager()
@receiver(post_save,sender=CustomUser)
def create_user_profile(sender,instance,created,**kwargs):
if created:
if instance.user_type == 1:
AdminHOD.objects.create(admin=instance)
if instance.user_type == 2:
Consultant.objects.create(admin=instance, client_id=Client.objects.get(id=1), tarif_Consultant="", tarif_facturation="", cnss="", rib="", paiement="")
@receiver(post_save,sender=CustomUser)
def save_user_profile(sender,instance,**kwargs):
if instance.user_type==1:
instance.adminhod.save()
if instance.user_type==2:
instance.consultant.save()
我的看法:
def add_staff_save(request):
if request.method!="POST":
return HttpResponse("Method Not Allowed")
else:
email=request.POST.get("email")
password=request.POST.get("password")
first_name=request.POST.get("first_name")
last_name=request.POST.get("last_name")
username=request.POST.get("username")
client_id=request.POST.get("client")
tarif_facturation=request.POST.get("tarif_facturation")
tarif_Consultant=request.POST.get("tarif_Consultant")
cnss = request.POST.get("cnss")
rib = request.POST.get("rib")
paiement = request.POST.get("paiement")
#try:
user=CustomUser.objects.create_user(username=username,password=password,email=email,last_name=last_name,first_name=first_name,user_type=2)
client_obj=Client.objects.get(id=client_id)
user.consultants.client_id=client_obj
user.consultant.tarif_facturation=tarif_facturation
user.consultant.tarif_Consultant=tarif_Consultant
user.consultant.cnss=cnss
user.consultant.rib=rib
user.consultant.paiement=paiement
user.save()
这是回溯:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'gestion']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'gestion.LoginCheckMiddleWare.LoginCheckMiddleWare']
Traceback (most recent call last):
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\fields\__init__.py", line 1774, in get_prep_value
return int(value)
The above exception (invalid literal for int() with base 10: '') was the direct cause of the following exception:
File "C:\Users\WD\Miniconda3\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
**File "C:\Users\WD\Desktop\weserveit\gestion\HodViews.py", line 95, in add_staff_save
user=CustomUser.objects.create_user(username=username,password=password,email=email,last_name=last_name,first_name=first_name,user_type=2)**
File "C:\Users\WD\Miniconda3\lib\site-packages\django\contrib\auth\models.py", line 146, in create_user
return self._create_user(username, email, password, **extra_fields)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\contrib\auth\models.py", line 140, in _create_user
user.save(using=self._db)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\contrib\auth\base_user.py", line 67, in save
super().save(*args, **kwargs)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\base.py", line 751, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\base.py", line 800, in save_base
update_fields=update_fields, raw=raw, using=using,
File "C:\Users\WD\Miniconda3\lib\site-packages\django\dispatch\dispatcher.py", line 179, in send
for receiver in self._live_receivers(sender)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\dispatch\dispatcher.py", line 179, in <listcomp>
for receiver in self._live_receivers(sender)
**File "C:\Users\WD\Desktop\weserveit\gestion\models.py", line 134, in create_user_profile
Consultant.objects.create(admin=instance, client_id=Client.objects.get(id=1), tarif_Consultant="", tarif_facturation="", cnss="", rib="", paiement="", profile_pic="")
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\manager.py", line 85, in manager_method**
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\query.py", line 447, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\base.py", line 751, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\base.py", line 789, in save_base
force_update, using, update_fields,
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\base.py", line 892, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\base.py", line 932, in _do_insert
using=using, raw=raw,
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\query.py", line 1249, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1394, in execute_sql
for sql, params in self.as_sql():
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1339, in as_sql
for obj in self.query.objs
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1339, in <listcomp>
for obj in self.query.objs
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1338, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1279, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\fields\__init__.py", line 823, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\fields\__init__.py", line 818, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\fields\__init__.py", line 1778, in get_prep_value
) from e
异常类型:/add_staff_save 处的值错误
异常值:字段 'tarif_facturation' 需要一个数字但得到的是 ''.
问题在于您在 post_save
中创建 Conslutant
对象
Consultant.objects.create(
admin=instance,
client_id=Client.objects.get(id=1),
tarif_Consultant="",
tarif_facturation="",
cnss="",
rib="",
paiement=""
)
tarif_facturation
是一个 IntegerField
但就在那里你的值是一个空字符串。将该值设置为数字,该错误将得到解决。
但是,对于您在 Consultant
创建中设置的许多其他属性,您所做的事情完全相同,因此只需确保您的值与模型所需的类型相匹配。
像这样;
Consultant.objects.create(
admin=instance,
client_id=Client.objects.get(id=1),
tarif_facturation=1,
)
这是我第一次在堆栈溢出中发布问题,所以请多多包涵 :D 我正在尝试从表单创建员工模型,但错误一直存在
这是我的模型文件:
class CustomUser(AbstractUser):
user_type_data = ((1, "HOD"), (2, "Consultant"))
user_type = models.CharField(default=1, choices=user_type_data, max_length=10)
class Consultant(models.Model):
CHOICES = (
('P', 'au paiement'),
('J15', 'J+15'),
('J30', 'J+30'),
)
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
client_id=models.ForeignKey(Client,on_delete=models.CASCADE,default=1)
tarif_facturation = models.IntegerField(blank=True, null=True)
tarif_Consultant = models.IntegerField(blank=True, null=True)
cnss = models.IntegerField(blank=True, null=True)
rib = models.IntegerField(blank=True, null=True)
paiement = models.CharField(max_length=300, choices=CHOICES)
profile_pic = models.FileField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
objects = models.Manager()
@receiver(post_save,sender=CustomUser)
def create_user_profile(sender,instance,created,**kwargs):
if created:
if instance.user_type == 1:
AdminHOD.objects.create(admin=instance)
if instance.user_type == 2:
Consultant.objects.create(admin=instance, client_id=Client.objects.get(id=1), tarif_Consultant="", tarif_facturation="", cnss="", rib="", paiement="")
@receiver(post_save,sender=CustomUser)
def save_user_profile(sender,instance,**kwargs):
if instance.user_type==1:
instance.adminhod.save()
if instance.user_type==2:
instance.consultant.save()
我的看法:
def add_staff_save(request):
if request.method!="POST":
return HttpResponse("Method Not Allowed")
else:
email=request.POST.get("email")
password=request.POST.get("password")
first_name=request.POST.get("first_name")
last_name=request.POST.get("last_name")
username=request.POST.get("username")
client_id=request.POST.get("client")
tarif_facturation=request.POST.get("tarif_facturation")
tarif_Consultant=request.POST.get("tarif_Consultant")
cnss = request.POST.get("cnss")
rib = request.POST.get("rib")
paiement = request.POST.get("paiement")
#try:
user=CustomUser.objects.create_user(username=username,password=password,email=email,last_name=last_name,first_name=first_name,user_type=2)
client_obj=Client.objects.get(id=client_id)
user.consultants.client_id=client_obj
user.consultant.tarif_facturation=tarif_facturation
user.consultant.tarif_Consultant=tarif_Consultant
user.consultant.cnss=cnss
user.consultant.rib=rib
user.consultant.paiement=paiement
user.save()
这是回溯:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'gestion']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'gestion.LoginCheckMiddleWare.LoginCheckMiddleWare']
Traceback (most recent call last):
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\fields\__init__.py", line 1774, in get_prep_value
return int(value)
The above exception (invalid literal for int() with base 10: '') was the direct cause of the following exception:
File "C:\Users\WD\Miniconda3\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
**File "C:\Users\WD\Desktop\weserveit\gestion\HodViews.py", line 95, in add_staff_save
user=CustomUser.objects.create_user(username=username,password=password,email=email,last_name=last_name,first_name=first_name,user_type=2)**
File "C:\Users\WD\Miniconda3\lib\site-packages\django\contrib\auth\models.py", line 146, in create_user
return self._create_user(username, email, password, **extra_fields)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\contrib\auth\models.py", line 140, in _create_user
user.save(using=self._db)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\contrib\auth\base_user.py", line 67, in save
super().save(*args, **kwargs)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\base.py", line 751, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\base.py", line 800, in save_base
update_fields=update_fields, raw=raw, using=using,
File "C:\Users\WD\Miniconda3\lib\site-packages\django\dispatch\dispatcher.py", line 179, in send
for receiver in self._live_receivers(sender)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\dispatch\dispatcher.py", line 179, in <listcomp>
for receiver in self._live_receivers(sender)
**File "C:\Users\WD\Desktop\weserveit\gestion\models.py", line 134, in create_user_profile
Consultant.objects.create(admin=instance, client_id=Client.objects.get(id=1), tarif_Consultant="", tarif_facturation="", cnss="", rib="", paiement="", profile_pic="")
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\manager.py", line 85, in manager_method**
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\query.py", line 447, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\base.py", line 751, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\base.py", line 789, in save_base
force_update, using, update_fields,
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\base.py", line 892, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\base.py", line 932, in _do_insert
using=using, raw=raw,
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\query.py", line 1249, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1394, in execute_sql
for sql, params in self.as_sql():
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1339, in as_sql
for obj in self.query.objs
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1339, in <listcomp>
for obj in self.query.objs
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1338, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1279, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\fields\__init__.py", line 823, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\fields\__init__.py", line 818, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\WD\Miniconda3\lib\site-packages\django\db\models\fields\__init__.py", line 1778, in get_prep_value
) from e
异常类型:/add_staff_save 处的值错误 异常值:字段 'tarif_facturation' 需要一个数字但得到的是 ''.
问题在于您在 post_save
Conslutant
对象
Consultant.objects.create(
admin=instance,
client_id=Client.objects.get(id=1),
tarif_Consultant="",
tarif_facturation="",
cnss="",
rib="",
paiement=""
)
tarif_facturation
是一个 IntegerField
但就在那里你的值是一个空字符串。将该值设置为数字,该错误将得到解决。
但是,对于您在 Consultant
创建中设置的许多其他属性,您所做的事情完全相同,因此只需确保您的值与模型所需的类型相匹配。
像这样;
Consultant.objects.create(
admin=instance,
client_id=Client.objects.get(id=1),
tarif_facturation=1,
)