1

I have this very simple view:

from models import Item, Tag, Category, User
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse

def save_item(request):
    try:
            print request.GET
        i = Item()
        i.user = User.objects.get_or_create(email=request.GET['user_email'][0])
        i.save()

        print i
    except Exception as e:
        print e.message()

    return HttpResponse()

with these very simple models:

class User(models.Model):
    email = models.EmailField()

class Item(models.Model):
    category = models.ForeignKey(Category, null=True, blank=True)
    tags = models.ManyToManyField(Tag, null=True, blank=True)
    address = models.CharField(max_length = 512, null=True, blank=True)
    user = models.ForeignKey(User)
    data = models.CharField(max_length = 1024, null=True, blank=True)

the print is the only thing that shows in my error.log:

[Wed May 16 01:23:40 2012] [error] <QueryDict: {u'website': [u''], u'comment': [u''], u'rating': [u''], u'phone number': [u''], u'address': [u''], u'user_email': [u'[email protected]']}>

but the Item model instance is not created!

I can manually create one in the admin or in the shell:

ubuntu@ip-10-196-47-228:~/WeShouldServer$ ./manage.py shell
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from RemoteStorage.models import Item
>>> i = Item()
>>> from RemoteStorage.models import User
>>> i.user = User.objects.get(pk=1)
>>> i.save()
# THIS WORKS

but trying to save one in the view just... fails. Silently. WHYYYY??!!!

5
  • On a side note, you have a risk for SQL injection by directly inputting user_email from the querystring, without cleaning it. Commented May 16, 2012 at 6:39
  • Won't your view break if user_email is an address that doesn't already exist in the database? Commented May 16, 2012 at 6:41
  • @Jordan, I know. I just want to get it up and running for the time being. Commented May 16, 2012 at 6:42
  • @user240515, nope, that's the point of get_or_create Commented May 16, 2012 at 6:43
  • 1
    @Jordan not true, the ORM ensures that everything is correctly escaped. Commented May 16, 2012 at 10:17

1 Answer 1

7

get_or_create returns tuple

 user, created = User.objects.get_or_create(email=request.GET['user_email'][0])
Sign up to request clarification or add additional context in comments.

2 Comments

DUHHHHHHHHHHHHH. Thanks. Any idea why the print statement didn't go in the error.log?
Print statements just go to standard output. You see them if you're running the development server. For logging, there's an entire chapter in Django's docs. Or you might have wanted this: how to print to stderr in python?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.