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'cc.emeraldeyes@gmail.com']}>

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??!!!

Colleen
  • 23,899
  • 12
  • 45
  • 75

1 Answers1

7

get_or_create returns tuple

 user, created = User.objects.get_or_create(email=request.GET['user_email'][0])
San4ez
  • 8,091
  • 4
  • 41
  • 62
  • DUHHHHHHHHHHHHH. Thanks. Any idea why the print statement didn't go in the error.log? – Colleen May 16 '12 at 06:41
  • 1
    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?](http://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python) – user240515 May 16 '12 at 06:50