python - get_or_create not creating object from POST form -
trying create object get_or_create(). response hits, never creates object.
python code:
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/')
so exception hits , never creates object. urls fine , i'm importing model. reason why object isn't being created? theres no errors, redirects normal.
traceback:
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 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 @ /post/ exception value: invalid literal int() base 10: 'text'
thank in advance.
i assume storing list of various note types in notetype model...
to make code work, first going need 1 of them model , set use when creating new 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)
notice change in get_or_create using note_type_orm , not note_type (which string)
Comments
Post a Comment