Django电商网站评论评分系统开发遇NOT NULL约束及WSGIRequest错误求助
Hey there! Let's work through those errors you're hitting with your Django e-commerce review system. I'll break down the most likely causes and fixes step by step.
First: Resolve the NOT NULL Constraint Failure
This error almost always ties back to your rating field in the Review model, or how you're handling its value when creating a review.
1. Fix the Model Field Definition
Looking at your code snippet, it looks like you started to add null=True but didn't finish it. For a field to allow empty values in the database, you need both blank=True (for form validation) and null=True (for database-level nullability). Update your model like this:
class Review(models.Model): product = models.ForeignKey(Product, on_delete=models.CASCADE) author = models.ForeignKey('auth.User', on_delete=models.CASCADE) title = models.CharField(max_length=100) content = models.TextField() # Complete the null=True setting rating = models.IntegerField(blank=True, null=True)
After updating, you'll need to apply database migrations to make this change take effect:
python manage.py makemigrations python manage.py migrate
2. Handle Empty Ratings in Forms & Views
If users can submit a review without a rating, make sure your form allows that. If you're using a ModelForm, add required=False to the rating field:
# reviews/forms.py from django import forms from .models import Review class ReviewForm(forms.ModelForm): class Meta: model = Review fields = ['title', 'content', 'rating'] widgets = { 'rating': forms.NumberInput(attrs={'min': 1, 'max': 5, 'required': False}), }
In your view, if you want to set a default rating when none is provided (like 0), add that logic before saving:
review = form.save(commit=False) # ... other assignments (product, author) if not review.rating: review.rating = 0 # Or skip this line to leave it as NULL review.save()
Next: Fix the WSGIRequest-Related Error
This usually happens when there's an issue with how you're handling the request object, specifically the request.user for the author field.
1. Ensure Users Are Logged In
Your author field is a foreign key to auth.User and doesn't allow null values. If an unauthenticated user tries to submit a review, request.user will be an AnonymousUser (not a valid User instance), which will throw an error. Add the @login_required decorator to your view:
# reviews/views.py from django.contrib.auth.decorators import login_required @login_required def add_review(request, product_id): # Your existing view logic here
2. Correctly Assign the Author
Double-check that you're properly setting the author field to request.user when creating the review. A common mistake is forgetting this step or assigning something else:
review = form.save(commit=False) review.product = product # Make sure this is correctly fetched from the URL/request review.author = request.user # Critical: assign the logged-in user as author review.save()
Quick Troubleshooting Checklist
- Did you run migrations after updating the model?
- Is your view decorated with
@login_requiredto prevent anonymous submissions? - Are you correctly passing
request.POSTto your form and validating it withform.is_valid()? - If you're not using a form, are you manually setting all required fields (including
authorandproduct) when creating the Review object?
内容的提问来源于stack exchange,提问作者ShavingSeagull

