Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4247630
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:08:01+00:00 2026-05-21T04:08:01+00:00

Working with Django 1.2 I am making a wine review site. A user should

  • 0

Working with Django 1.2 I am making a wine review site. A user should only be able to review each wine once, but should be able to go back and re-review a wine without raising an error.

Using the get_or_create method seems the most rational solution but I have been running into various problems implementing it. Searching I found this article which looked promising:
Correct way to use get_or_create?

and of course the django documentation on it:
http://docs.djangoproject.com/en/1.2/ref/models/querysets/#get-or-create

But didn’t seem to answer my question. Here is my code:

Views.py

@login_required
def wine_review_page(request, wine_id):
wine = get_object_or_404(Wine, pk=wine_id)

if request.method == 'POST':
form = WineReviewForm(request.POST)
if form.is_valid():
  review, created = Review.objects.get_or_create(
    user = request.user,
    wine = wine,
    like_dislike = form.cleaned_data['like_dislike'],
    ...
    )
variables = RequestContext(request, {
 'wine': wine
  })   
  review.save()
  return HttpResponseRedirect(
    '/detail/%s/' % wine_id
  )
else:
  form = WineReviewForm()
  variables = RequestContext(request, {
  'form': form,
  'wine': wine
 })
return render_to_response('wine_review_page.html', variables)

Models.py

class Review(models.Model):
  wine = models.ForeignKey(Wine, unique=True)
  user = models.ForeignKey(User, unique=True)
  like_dislike = models.CharField(max_length=7, unique=True)
  ...

If I understand how to use get_or_create correctly, since I am not matching on all the values like_dislike, etc… then django perceives it to be unique. I tried removing the other form parameters, but then they are not submitted with the post request.

Suggestions would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:08:02+00:00Added an answer on May 21, 2026 at 4:08 am

    I came across this too when making a CRUD based app. I’m not sure if there’s a better way but the way I ended up getting doing was using a exists() to check if an entry … exists.

    You can use get_or_create within the is_valid() scope, however, you need to check if the review exists before displaying your form in order to load instance data into the form in the case that the review already exists.

    Your models.py might look like this:

    from django.db import models
    from django.contrib.auth.models import User
    
    class Wine(models.Model):
        name = models.CharField()
    
    class Review(models.Model):
        wine = models.ForeignKey(Wine)
        user = models.ForeignKey(User)
        like = models.BooleanField(null=True, blank=True) # if null, unrated
    

    Your forms.py might look like this:

    from django import forms
    
    class WineReviewForm(forms.ModelForm):
        class Meta:
            model = Review
            fields = ['like',] # excludes the user and wine field from the form
    

    Using get_or_create will let you do this if used like so:

    @login_required
    def wine_review_page(request, wine_id):
        wine = get_object_or_404(Wine, pk=wine_id)
    
        review, created = Review.objects.get_or_create(user=request.user, wine=wine)
    
        if request.method == 'POST':
            form = WineReviewForm(request.POST, instance=review)
            if form.is_valid():
                form.save()   
                return HttpResponseRedirect('/detail/%s/' % wine_id )
        else:
            form = WineReviewForm(instance=review)
    
        variables = RequestContext(request, {'form': form, 'wine': wine })
        return render_to_response('wine_review_page.html', variables) 
    

    Doing creates a review just by visiting the page and requires that the other information either have a default or are allowed to be blank at the model level.

    With exists(), you get two db hits if the review exists, however you don’t create an object unless the user submits a valid form:

    @login_required
    def wine_review_page(request, wine_id):
        wine = get_object_or_404(Wine, pk=wine_id)
    
        review = None
        if Review.objects.filter(user=request.user, wine=wine).exists():
            review = Review.objects.get(user=request.user, wine=wine)
    
        if request.method == 'POST':
            form = WineReviewForm(request.POST, instance=review)
            if form.is_valid():
                form.save()   
                return HttpResponseRedirect('/detail/%s/' % wine_id )
        else:
            form = WineReviewForm(instance=review)
    
        variables = RequestContext(request, {'form': form, 'wine': wine })
        return render_to_response('wine_review_page.html', variables)
    

    I used exists() but I think that this might be better?

    try:
        review = Review.objects.get(user=request.user, wine=wine)
    except Review.DoesNotExist:
        review = None
    

    Hopefully someone with more experience will chime in.


    Edit:

    Here is a fairly old post from Daniel Roseman’s Blog. I don’t know if it is still applicable, but might be relevant to your question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really love working with Django and Python, when I go back to PHP
I'm working on a django app for a site that requires an image gallery.
I'm working on a Django site, using the Django 1.4 official release. My site
I am working with a Django model that looks like this: class Subscription(models.Model): user
I'm working on Django website that should give a possibility to select cooking recipes
I am working on Django application where users can exchange post-it notes with each
We're all development team working on a Django site. Recently we've begun using zc.buildout
I'm developing a Django site. I'm making all my changes on the live server,
I started working on Django 1.0 Web Site Development and have gotten my server
I've been working with Django for a while now (currently on version 1.2), but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.