2013-07-21 94 views
2

嘗試使用get_or_create()創建對象。響應命中,但它永遠不會成功創建該對象。get_or_create不從POST表單創建對象

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/') 

所以異常不斷命中,從不創建對象。網址很好,我正在導入模型。任何理由爲什麼這個對象沒有被創建? Theres沒有錯誤,它只是正常重定向。

回溯:

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' 

感謝您對您的幫助提前。

+0

@AdamKG返回'無效字面INT ()以10爲底:'text'' ..? – Modelesq

+0

@Modelesq使用您當前的代碼更新問題,幷包含完整的回溯(在錯誤頁面中應該有一個「切換到複製/粘貼視圖」)。 – AdamKG

+0

@AdamKG更新! – Modelesq

回答

0

我認爲你是存儲在NoteType模型中的各種注意類型的列表...

爲了使此代碼工作,您首先將需要得到他們的一個從模型,然後設置使用創建新的筆記

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在get_or_create的變化,而不僅僅是note_type(這僅僅是一個字符串)時