所以我目前正在为我的学校项目建立一个网上商店。在网上商店,用户应该能够提交对每个产品的评论。但我有问题时,试图提交评论。它给我这个错误:
NOT NULL constraint failed:
我已经被困在这个问题上大约15个小时了,我似乎无法解决它。我将感谢所有的帮助,我可以得到。
追溯:
Traceback (most recent call last):
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
The above exception (NOT NULL constraint failed: products_reviewrating.user_id) was the direct cause of the following exception:
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/workspace/webster00/products/views.py", line 91, in submit_review
newreview.save()
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/models/base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/models/base.py", line 763, in save_base
updated = self._save_table(
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/models/base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/models/base.py", line 906, in _do_insert
return manager._insert(
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/models/query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1410, in execute_sql
cursor.execute(sql, params)
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
Exception Type: IntegrityError at /products/submit_review/6/
Exception Value: NOT NULL constraint failed: products_reviewrating.user_id
Views.py
def submit_review(request, product_id):
print(f'product_id: {product_id}')
url = request.META.get('HTTP_REFERER')
if request.method == 'POST':
try:
product = Product.objects.get(id=product_id)
reviews = ReviewRating.objects.filter(user=request.user.userprofile, product=product).first()
form = ReviewForm(request.POST, instance=reviews)
newreview = form.save(commit=False)
newreview.product = product
newreview.save()
form.save()
messages.success(request, 'Thank you! Your review has been updated')
return redirect(url)
except ReviewRating.DoesNotExist:
form = ReviewForm(request.POST)
if form.is_valid():
data = ReviewRating()
data.subject = form.cleaned_data['subject']
data.rating = form.cleaned_data['rating']
data.review = form.cleaned_data['review']
data.ip = request.META.get('REMOTE_ADDR')
data.product_id = product_id
data.user_id = request.user.id
data.save()
messages.success(request, 'Thank you! Your review has been submited.')
return redirect(url)
审查也没有显示在django管理中。
2条答案
按热度按时间woobm2wo1#
在
newreview
的情况下,您没有分配所有字段(在本例中为user
)。加
在
save
之前-您可能需要对尚未处理的其他字段重复此操作。vof42yt12#
您可能可以使用以下方法大大简化视图:
Note:您可以使用**
@login_required
decorator [Django-doc]将视图限制为只对经过身份验证的用户开放。注意:使用
get_object_or_404(…)
[Django-doc]通常比直接使用.get(…)
**[Django-doc]更好。如果对象不存在,例如因为用户自己更改了URL,get_object_or_404(…)
将导致返回 HTTP 404 Not Found 响应,而使用.get(…)
将导致 HTTP 500 Server Error。