chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


RelatedObjectDoesNotExist at /review/add/1/ filter_list
Author
Message
RelatedObjectDoesNotExist at /review/add/1/ #1
Hi!
I have a review app where a user gets to review a restaurant. I have created a form for review and views but I am getting an error submitting the form to review a restaurant. I get a RelatedObjectDoesNotExist at /review/add/1/ with an exception of review has no restaurant.

Following is my model for review:
Code:
class Review(models.Model):
   user = models.ForeignKey(User)
   restaurant = models.ForeignKey(Restaurant)
   review = models.TextField()
   created = models.DateTimeField(auto_now_add=True)
   updated = models.DateTimeField(auto_now=True)

forms.py
Code:
class ReviewForm(ModelForm):
   class Meta:
       model = Review
       fields = ['review']
       widget = {
           'review':Textarea(attrs={'cols':40, 'rows':10})
       }  

views.py
Code:
def add_review(request, review_id):
   restaurant = get_object_or_404(Restaurant, pk=review_id)
   print('restaurant from review form',restaurant)
   if request.method == 'POST':
       form = ReviewForm(request.POST)
       if form.is_valid():
           review = form.cleaned_data['review']
           review = form.save(commit=False)
           Review.objects.create(user=request.user,restaurant=restaurant,review=review)
           return redirect('/restaurants/')
   else:
       form = ReviewForm()
   return render(request, 'review/review_form.html', {'form':form})

Any ideas? I'm clueless... Thanks!

Reply

RE: RelatedObjectDoesNotExist at /review/add/1/ #2
I'm not sure, because pyhton isn't my usual programming language, but I think that you need to set up a relation with the object. It can't find it because they aren't connected yet. Don't take my word for it, I'm not sure.
[Image: 5u8rTPk.jpg]
Click image to go to my website, it has been updated!

Reply






Users browsing this thread: 1 Guest(s)