get_or_create没有从POST表单创建对象

时间:2013-07-21 22:24:59

标签: python django

尝试使用get_or_create()创建对象。响应命中,但它从未成功创建对象。

Python代码:

class Note(models.Model):
     user = models.ForeignKey(User)
     topic = models.CharField(max_length=500, null=True, blank=True)
     created = models.DateTimeField(auto_now_add=True)
     content = models.TextField(null=True, blank=True)
     type = models.ForeignKey(NoteType, null=True, blank=True)

    def __unicode__(self):
        return self.topic

def post(request):
     topic = request.POST['topic']
     user = request.user 
     content = request.POST['content']
     note_type = request.POST['type']

     note, created = Note.objects.get_or_create(user=user, topic=topic, content=content, type=note_type)

     return HttpResponseRedirect('/home/')

因此异常不断地命中并且永远不会创建对象。网址很好,我正在导入模型。没有创建此对象的任何原因?没有错误,它只是正常重定向。

回溯:

Environment:


Request Method: POST
Request URL: http://localhost:8000/post/

Django Version: 1.5.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'notes')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "c:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "c:\Python27\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  25.                 return view_func(request, *args, **kwargs)
File "c:\Users\Alana\project-fridge\notes\views.py" in post_note
  34.       note, created = Note.objects.get_or_create(user=user, topic=topic, content=content, type=note_type)
File "c:\Python27\lib\site-packages\django\db\models\manager.py" in get_or_create
  146.         return self.get_query_set().get_or_create(**kwargs)
File "c:\Python27\lib\site-packages\django\db\models\query.py" in get_or_create
  470.             return self.get(**lookup), False
File "c:\Python27\lib\site-packages\django\db\models\query.py" in get
  379.         clone = self.filter(*args, **kwargs)
File "c:\Python27\lib\site-packages\django\db\models\query.py" in filter
  655.         return self._filter_or_exclude(False, *args, **kwargs)
File "c:\Python27\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  673.             clone.query.add_q(Q(*args, **kwargs))
File "c:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_q
  1266.                             can_reuse=used_aliases, force_having=force_having)
File "c:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_filter
  1197.                 connector)
File "c:\Python27\lib\site-packages\django\db\models\sql\where.py" in add
  71.             value = obj.prepare(lookup_type, value)
File "c:\Python27\lib\site-packages\django\db\models\sql\where.py" in prepare
  339.             return self.field.get_prep_lookup(lookup_type, value)
File "c:\Python27\lib\site-packages\django\db\models\fields\related.py" in get_prep_lookup
  143.             return self._pk_trace(value, 'get_prep_lookup', lookup_type)
File "c:\Python27\lib\site-packages\django\db\models\fields\related.py" in _pk_trace
  216.         v = getattr(field, prep_func)(lookup_type, v, **kwargs)
File "c:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_lookup
  322.             return self.get_prep_value(value)
File "c:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
  555.         return int(value)

Exception Type: ValueError at /post/
Exception Value: invalid literal for int() with base 10: 'text'

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我假设您在NoteType模型中存储了各种Note类型的列表...

要使此代码正常工作,您首先需要从该模型中获取其中一个,然后在创建新笔记时设置使用该代码

note_type = request.POST['type']

note_type_orm = NoteType.objects.get(type=note_type)

note, created = Note.objects.get_or_create(user=user, topic=topic, content=content, type=note_type_orm)

注意我现在使用note_type_orm而不仅仅是note_type(只是一个字符串)的get_or_create中的更改