Django 向模型添加新对象将返回';列表';对象没有属性'_承诺';错误
我想向数据库中的模型添加一个新对象,但当我尝试添加它时,它会给出一个错误,如“list”对象没有属性“\u committed” 它在汽车模型.save()部分给出了一个错误 回溯错误Django 向模型添加新对象将返回';列表';对象没有属性'_承诺';错误,django,django-models,Django,Django Models,我想向数据库中的模型添加一个新对象,但当我尝试添加它时,它会给出一个错误,如“list”对象没有属性“\u committed” 它在汽车模型.save()部分给出了一个错误 回溯错误 Environment: Request Method: POST Request URL: http://127.0.0.1:8000/cars/add_car Django Version: 3.1.7 Python Version: 3.9.0 Installed Applications: ['ca
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/cars/add_car
Django Version: 3.1.7
Python Version: 3.9.0
Installed Applications:
['cars.apps.CarsConfig',
'pages.apps.PagesConfig',
'accounts.apps.AccountsConfig',
'contact.apps.ContactConfig',
'houses.apps.HousesConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ckeditor',
'django.contrib.humanize',
'django.contrib.sites']
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']
Traceback (most recent call last):
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\gx760_000\Desktop\sahibinden\cars\views.py", line 434, in add_car
car_model.save()
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\db\models\base.py", line 753, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\db\models\base.py", line 790, in save_base
updated = self._save_table(
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\db\models\base.py", line 895, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\db\models\base.py", line 933, in _do_insert
return manager._insert(
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\db\models\query.py", line 1254, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\db\models\sql\compiler.py", line 1396, in execute_sql
for sql, params in self.as_sql():
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\db\models\sql\compiler.py", line 1339, in as_sql
value_rows = [
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\db\models\sql\compiler.py", line 1340, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\db\models\sql\compiler.py", line 1340, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\db\models\sql\compiler.py", line 1291, in pre_save_val
return field.pre_save(obj, add=True)
File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\db\models\fields\files.py", line 305, in pre_save
if file and not file._committed:
Exception Type: AttributeError at /cars/add_car
Exception Value: 'list' object has no attribute '_committed'
我没有使用表单,因为有多个选择字段。我需要在没有表单的情况下编写代码。我的错误在哪里?如何在不使用表单的情况下修复它?是否覆盖了“车型保存”方法?如果是,请添加详细信息。您的意思是什么that@RedAndBlack您对所有文件输入都使用了
request.FILES.getlist
,而文件字段只能保存一个文件。@AbdulAzizBarkat非常感谢,解决了我的问题。我更改了所有getlist以获取,因此它解决了问题
def add_car(request):
if request.method == 'POST' and request.FILES:
user_id = request.POST['user_id']
brand = request.POST['brand']
status_of_car = request.POST['status_of_car']
city = request.POST['city']
ilce = request.POST['ilce']
e_mail = request.POST['e_mail']
phone_number = request.POST['phone_number']
car_title = request.POST['car_title']
description = request.POST['description']
price = request.POST['price']
serial = request.POST['serial']
color = request.POST['color']
model = request.POST['model']
year = request.POST['year']
condition = request.POST['condition']
body_style = request.POST['body_style']
engine = request.POST['engine']
transmission = request.POST['transmission']
interior = request.POST['interior']
kilometers = request.POST['kilometers']
passengers = request.POST['passengers']
power_of_engine = request.POST['power_of_engine']
fuel_type = request.POST['fuel_type']
no_of_owners = request.POST['no_of_owners']
car_photo = request.FILES.getlist('car_photo')
car_photo_1 = request.FILES.getlist('car_photo_1')
car_photo_2 = request.FILES.getlist('car_photo_2')
car_photo_3 = request.FILES.getlist('car_photo_3')
car_photo_4 = request.FILES.getlist('car_photo_4')
car_photo_5 = request.FILES.getlist('car_photo_5')
car_photo_6 = request.FILES.getlist('car_photo_6')
car_photo_7 = request.FILES.getlist('car_photo_7')
car_photo_8 = request.FILES.getlist('car_photo_8')
car_photo_9 = request.FILES.getlist('car_photo_9')
car_photo_10 = request.FILES.getlist('car_photo_10')
car_photo_11 = request.FILES.getlist('car_photo_11')
car_photo_12 = request.FILES.getlist('car_photo_12')
car_photo_13 = request.FILES.getlist('car_photo_13')
car_photo_14 = request.FILES.getlist('car_photo_14')
feature_1 = request.POST.get('feature_1')
feature_2 = request.POST.get('feature_2')
feature_3 = request.POST.get('feature_3')
feature_4 = request.POST.get('feature_4')
feature_5 = request.POST.get('feature_5')
feature_6 = request.POST.get('feature_6')
feature_7 = request.POST.get('feature_7')
feature_8 = request.POST.get('feature_8')
feature_9 = request.POST.get('feature_9')
feature_10 = request.POST.get('feature_10')
feature_11 = request.POST.get('feature_11')
feature_12 = request.POST.get('feature_12')
feature_13 = request.POST.get('feature_13')
feature_14 = request.POST.get('feature_14')
feature_15 = request.POST.get('feature_15')
feature_16 = request.POST.get('feature_16')
feature_17 = request.POST.get('feature_17')
feature_18 = request.POST.get('feature_18')
feature_19 = request.POST.get('feature_19')
feature_20 = request.POST.get('feature_20')
feature_21 = request.POST.get('feature_21')
feature_22 = request.POST.get('feature_22')
feature_23 = request.POST.get('feature_23')
feature_24 = request.POST.get('feature_24')
feature_25 = request.POST.get('feature_25')
feature_26 = request.POST.get('feature_26')
feature_27 = request.POST.get('feature_27')
feature_28 = request.POST.get('feature_28')
feature_29 = request.POST.get('feature_29')
feature_30 = request.POST.get('feature_30')
feature_31 = request.POST.get('feature_31')
feature_32 = request.POST.get('feature_32')
hardware_feature_1 = request.POST.get('hardware_feature_1')
hardware_feature_2 = request.POST.get('hardware_feature_2')
hardware_feature_3 = request.POST.get('hardware_feature_3')
hardware_feature_4 = request.POST.get('hardware_feature_4')
hardware_feature_5 = request.POST.get('hardware_feature_5')
hardware_feature_6 = request.POST.get('hardware_feature_6')
hardware_feature_7 = request.POST.get('hardware_feature_7')
hardware_feature_8 = request.POST.get('hardware_feature_8')
hardware_feature_9 = request.POST.get('hardware_feature_9')
hardware_feature_10 = request.POST.get('hardware_feature_10')
hardware_feature_11 = request.POST.get('hardware_feature_11')
hardware_feature_12 = request.POST.get('hardware_feature_12')
hardware_feature_13 = request.POST.get('hardware_feature_13')
hardware_feature_14 = request.POST.get('hardware_feature_14')
hardware_feature_15 = request.POST.get('hardware_feature_15')
hardware_feature_16 = request.POST.get('hardware_feature_16')
hardware_feature_17 = request.POST.get('hardware_feature_17')
hardware_feature_18 = request.POST.get('hardware_feature_18')
hardware_feature_19 = request.POST.get('hardware_feature_19')
hardware_feature_20 = request.POST.get('hardware_feature_20')
hardware_feature_21 = request.POST.get('hardware_feature_21')
hardware_feature_22 = request.POST.get('hardware_feature_22')
hardware_feature_23 = request.POST.get('hardware_feature_23')
hardware_feature_24 = request.POST.get('hardware_feature_24')
hardware_feature_25 = request.POST.get('hardware_feature_25')
hardware_feature_26 = request.POST.get('hardware_feature_26')
hardware_feature_27 = request.POST.get('hardware_feature_27')
hardware_feature_28 = request.POST.get('hardware_feature_28')
hardware_feature_29 = request.POST.get('hardware_feature_29')
hardware_feature_30 = request.POST.get('hardware_feature_30')
hardware_feature_31 = request.POST.get('hardware_feature_31')
hardware_feature_32 = request.POST.get('hardware_feature_32')
hardware_feature_33 = request.POST.get('hardware_feature_33')
hardware_feature_34 = request.POST.get('hardware_feature_34')
hardware_feature_35 = request.POST.get('hardware_feature_35')
hardware_feature_36 = request.POST.get('hardware_feature_36')
ex_hardware_feature_1 = request.POST.get('ex_hardware_feature_1')
ex_hardware_feature_2 = request.POST.get('ex_hardware_feature_2')
ex_hardware_feature_3 = request.POST.get('ex_hardware_feature_3')
ex_hardware_feature_4 = request.POST.get('ex_hardware_feature_4')
ex_hardware_feature_5 = request.POST.get('ex_hardware_feature_5')
ex_hardware_feature_6 = request.POST.get('ex_hardware_feature_6')
ex_hardware_feature_7 = request.POST.get('ex_hardware_feature_7')
ex_hardware_feature_8 = request.POST.get('ex_hardware_feature_8')
ex_hardware_feature_9 = request.POST.get('ex_hardware_feature_9')
ex_hardware_feature_10 = request.POST.get('ex_hardware_feature_10')
ex_hardware_feature_11 = request.POST.get('ex_hardware_feature_11')
ex_hardware_feature_12 = request.POST.get('ex_hardware_feature_12')
ex_hardware_feature_13 = request.POST.get('ex_hardware_feature_13')
ex_hardware_feature_14 = request.POST.get('ex_hardware_feature_14')
ex_hardware_feature_15 = request.POST.get('ex_hardware_feature_15')
ex_hardware_feature_16 = request.POST.get('ex_hardware_feature_16')
ex_hardware_feature_17 = request.POST.get('ex_hardware_feature_17')
ex_hardware_feature_18 = request.POST.get('ex_hardware_feature_18')
ex_hardware_feature_19 = request.POST.get('ex_hardware_feature_19')
ex_hardware_feature_20 = request.POST.get('ex_hardware_feature_20')
ex_hardware_feature_21 = request.POST.get('ex_hardware_feature_21')
ex_hardware_feature_22 = request.POST.get('ex_hardware_feature_22')
ex_hardware_feature_23 = request.POST.get('ex_hardware_feature_23')
ex_hardware_feature_24 = request.POST.get('ex_hardware_feature_24')
multimedia_feature_1 = request.POST.get('multimedia_feature_1')
multimedia_feature_2 = request.POST.get('multimedia_feature_2')
multimedia_feature_3 = request.POST.get('multimedia_feature_3')
multimedia_feature_4 = request.POST.get('multimedia_feature_4')
multimedia_feature_5 = request.POST.get('multimedia_feature_5')
multimedia_feature_6 = request.POST.get('multimedia_feature_6')
multimedia_feature_7 = request.POST.get('multimedia_feature_7')
multimedia_feature_8 = request.POST.get('multimedia_feature_8')
multimedia_feature_9 = request.POST.get('multimedia_feature_9')
multimedia_feature_10 = request.POST.get('multimedia_feature_10')
multimedia_feature_11 = request.POST.get('multimedia_feature_11')
multimedia_feature_12 = request.POST.get('multimedia_feature_12')
multimedia_feature_13 = request.POST.get('multimedia_feature_13')
is_featured = request.POST.get('is_featured')
car_model = Car(user_id=user_id,
brand=brand,
status_of_car=status_of_car,
city=city,
ilce=ilce,
e_mail=e_mail,
phone_number=phone_number,
car_title=car_title,
description=description,
price=price,
serial=serial,
color=color,
model=model,
year=year,
condition=condition,
body_style=body_style,
engine=engine,
transmission=transmission,
interior=interior,
kilometers=kilometers,
passengers=passengers,
power_of_engine=power_of_engine,
fuel_type=fuel_type,
no_of_owners=no_of_owners,
car_photo=car_photo,
car_photo_1=car_photo_1,
car_photo_2=car_photo_2,
car_photo_3=car_photo_3,
car_photo_4=car_photo_4,
car_photo_5=car_photo_5,
car_photo_6=car_photo_6,
car_photo_7=car_photo_7,
car_photo_8=car_photo_8,
car_photo_9=car_photo_9,
car_photo_10=car_photo_10,
car_photo_11=car_photo_11,
car_photo_12=car_photo_12,
car_photo_13=car_photo_13,
car_photo_14=car_photo_14,
feature_1=feature_1,
feature_2=feature_2,
feature_3=feature_3,
feature_4=feature_4,
feature_5=feature_5,
feature_6=feature_6,
feature_7=feature_7,
feature_8=feature_8,
feature_9=feature_9,
feature_10=feature_10,
feature_11=feature_11,
feature_12=feature_12,
feature_13=feature_13,
feature_14=feature_14,
feature_15=feature_15,
feature_16=feature_16,
feature_17=feature_17,
feature_18=feature_18,
feature_19=feature_19,
feature_20=feature_20,
feature_21=feature_21,
feature_22=feature_22,
feature_23=feature_23,
feature_24=feature_24,
feature_25=feature_25,
feature_26=feature_26,
feature_27=feature_27,
feature_28=feature_28,
feature_29=feature_29,
feature_30=feature_30,
feature_31=feature_31,
feature_32=feature_32,
hardware_feature_1=hardware_feature_1,
hardware_feature_2=hardware_feature_2,
hardware_feature_3=hardware_feature_3,
hardware_feature_4=hardware_feature_4,
hardware_feature_5=hardware_feature_5,
hardware_feature_6=hardware_feature_6,
hardware_feature_7=hardware_feature_7,
hardware_feature_8=hardware_feature_8,
hardware_feature_9=hardware_feature_9,
hardware_feature_10=hardware_feature_10,
hardware_feature_11=hardware_feature_11,
hardware_feature_12=hardware_feature_12,
hardware_feature_13=hardware_feature_13,
hardware_feature_14=hardware_feature_14,
hardware_feature_15=hardware_feature_15,
hardware_feature_16=hardware_feature_16,
hardware_feature_17=hardware_feature_17,
hardware_feature_18=hardware_feature_18,
hardware_feature_19=hardware_feature_19,
hardware_feature_20=hardware_feature_20,
hardware_feature_21=hardware_feature_21,
hardware_feature_22=hardware_feature_22,
hardware_feature_23=hardware_feature_23,
hardware_feature_24=hardware_feature_24,
hardware_feature_25=hardware_feature_25,
hardware_feature_26=hardware_feature_26,
hardware_feature_27=hardware_feature_27,
hardware_feature_28=hardware_feature_28,
hardware_feature_29=hardware_feature_29,
hardware_feature_30=hardware_feature_30,
hardware_feature_31=hardware_feature_31,
hardware_feature_32=hardware_feature_32,
hardware_feature_33=hardware_feature_33,
hardware_feature_34=hardware_feature_34,
hardware_feature_35=hardware_feature_35,
hardware_feature_36=hardware_feature_36,
ex_hardware_feature_1=ex_hardware_feature_1,
ex_hardware_feature_2=ex_hardware_feature_2,
ex_hardware_feature_3=ex_hardware_feature_3,
ex_hardware_feature_4=ex_hardware_feature_4,
ex_hardware_feature_5=ex_hardware_feature_5,
ex_hardware_feature_6=ex_hardware_feature_6,
ex_hardware_feature_7=ex_hardware_feature_7,
ex_hardware_feature_8=ex_hardware_feature_8,
ex_hardware_feature_9=ex_hardware_feature_9,
ex_hardware_feature_10=ex_hardware_feature_10,
ex_hardware_feature_11=ex_hardware_feature_11,
ex_hardware_feature_12=ex_hardware_feature_12,
ex_hardware_feature_13=ex_hardware_feature_13,
ex_hardware_feature_14=ex_hardware_feature_14,
ex_hardware_feature_15=ex_hardware_feature_15,
ex_hardware_feature_16=ex_hardware_feature_16,
ex_hardware_feature_17=ex_hardware_feature_17,
ex_hardware_feature_18=ex_hardware_feature_18,
ex_hardware_feature_19=ex_hardware_feature_19,
ex_hardware_feature_20=ex_hardware_feature_20,
ex_hardware_feature_21=ex_hardware_feature_21,
ex_hardware_feature_22=ex_hardware_feature_22,
ex_hardware_feature_23=ex_hardware_feature_23,
ex_hardware_feature_24=ex_hardware_feature_24,
multimedia_feature_1=multimedia_feature_1,
multimedia_feature_2=multimedia_feature_2,
multimedia_feature_3=multimedia_feature_3,
multimedia_feature_4=multimedia_feature_4,
multimedia_feature_5=multimedia_feature_5,
multimedia_feature_6=multimedia_feature_6,
multimedia_feature_7=multimedia_feature_7,
multimedia_feature_8=multimedia_feature_8,
multimedia_feature_9=multimedia_feature_9,
multimedia_feature_10=multimedia_feature_10,
multimedia_feature_11=multimedia_feature_11,
multimedia_feature_12=multimedia_feature_12,
multimedia_feature_13=multimedia_feature_13,
is_featured=is_featured,)
car_model.save()
return redirect('cars')
return render(request, 'cars/add_car.html')