我有一张表格可以跟踪销售顾问的潜在客户和当前交易。但是,我陷入了创建交易的过程中,因为django一直给我错误" ValueError:对于带有基数10的int()的无效文字:"当我尝试用字符串输入客户姓氏时。当我尝试使用整数lastname创建时,表单工作。
Models.py
class Customer(models.Model):
name = models.CharField(max_length = 20)
surname = models.CharField(max_length = 20,blank = True)
nick_name = models.CharField(max_length = 20,blank = True)
phone_regex = RegexValidator(regex = r'^\+?\d{10,15}$',message = "Phone number invalid")
phone_number = models.CharField(validators=[phone_regex],max_length=16)
second_phone_number = models.CharField(validators=[phone_regex],max_length=16,blank = True)
date_created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
class Deal(models.Model):
name = models.CharField(max_length = 50)
lastname = models.CharField(max_length = 50,blank=True)
booked_car = models.ForeignKey('BookedCar', on_delete = models.SET_NULL,
null = True)
prospect = models.ForeignKey('Customer', on_delete = models.SET_NULL, null = True)
is_credit = models.BooleanField()
car_price = models.PositiveIntegerField()
final_accessories = models.PositiveIntegerField()
final_campaign = models.PositiveIntegerField()
final_spending = models.PositiveIntegerField()
views.py
def booking_create(request,customer_id):
customer = get_object_or_404(Customer,pk=customer_id)
template_name = 'customerDB/booking_create.html'
if request.method == 'POST':
form = BookingCreate(request.POST)
if form.is_valid():
customer.deal_set.create(name = form.cleaned_data['name'],
lastname = form.cleaned_data['lastname'],
booked_car = form.cleaned_data['booked_car'],
is_credit = form.cleaned_data['is_credit'],
car_price = form.cleaned_data['car_price'],
final_accessories = form.cleaned_data['lastname'],
final_campaign = form.cleaned_data['final_campaign'],
final_spending = form.cleaned_data['final_spending']
)
else:
form = BookingCreate(initial= {'name':customer.name})
return render(request,template_name,{'form':form})
forms.py
class BookingCreate(ModelForm):
class Meta:
model = Deal
fields = '__all__'
urls.py
urlpatterns = [
path('<int:customer_id>/',views.detail, name='detail'),
path('<int:customer_id>/follow_up/', views.follow_up,name = 'follow_up'),
path('<int:customer_id>/transfer/',views.transfer_view,name= 'transfer_view'),
path('<int:customer_id>/booking/',views.booking_create,name= 'booking_create'),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
booking_create.html
{% block content %}
<html>
<form action="" method="post">
{%csrf_token %}
<h3>booking detail</h3>
<br/>
<table>
{{form.as_table}}
</table>
<br/>
<input type = "submit" value="Submit" class="btn btn-primary"/>
</form>
{% endblock %}
完整追溯
Traceback (most recent call last):
File "C:\Users\Top\project\myvenv\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Top\project\mysite\customerDB\views.py", line 199, in booking_create
final_spending = form.cleaned_data['final_spending']
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\fields\related_descriptors.py", line 623, in create
return super(RelatedManager, self.db_manager(db)).create(**kwargs)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\query.py", line 417, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\base.py", line 729, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\base.py", line 759, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\base.py", line 842, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\base.py", line 880, in _do_insert
using=using, raw=raw)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\query.py", line 1125, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\sql\compiler.py", line 1280, in execute_sql
for sql, params in self.as_sql():
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\sql\compiler.py", line 1233, in as_sql
for obj in self.query.objs
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\sql\compiler.py", line 1233, in <listcomp>
for obj in self.query.objs
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\sql\compiler.py", line 1232, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\sql\compiler.py", line 1172, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\fields\__init__.py", line 767, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\fields\__init__.py", line 762, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\Top\project\myvenv\lib\site-packages\django\db\models\fields\__init__.py", line 1826, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Bush'
答案 0 :(得分:1)
错误几乎肯定来自这条线:
final_accessories = form.cleaned_data['lastname'],
因为final_accessories
应该是一个整数,但是你传递的是lastname的值。
为了避免这样的问题,您应该真正依赖ModelForm为您创建整个实例的能力。您可以通过commit=False
方法分配客户实例:
if form.is_valid():
deal = form.save(commit=False)
deal.prospect = customer
deal.save()