关于python:django模型实例未保存但未引发错误

Django model instance not saving but no errors thrown

我有一个非常简单的观点:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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()

有了这些非常简单的模型:

1
2
3
4
5
6
7
8
9
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)

打印是我的error.log中唯一显示的内容:

1
[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]']}>

但没有创建项目模型实例!

我可以在管理或shell中手动创建一个:

1
2
3
4
5
6
7
8
9
10
11
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

但是试图在视图中保存一个…失败。默默地。为什么??!!!!


get_or_create返回tuple

1
 user, created = User.objects.get_or_create(email=request.GET['user_email'][0])