Opened 18 years ago
Closed 18 years ago
#3342 closed (duplicate)
ForeignKey relation doesn't work with newforms form_for_model() save
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | ForeignKey newforms | |
Cc: | gandalf@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When having a model with ForeignKey(), clean_data returns unicoded integer of related-field, but django expects an instance of that model. Because of that model_save in newforms/models.py line 18, obj = self._model(self.clean_data) fails.
The solution is probably to resolve ForeignKey somewhere before but I'm not sure how to do this properly.
Change History (4)
follow-up: 2 comment:1 by , 18 years ago
comment:2 by , 18 years ago
models.py: (taken from models test)
from django.db import models class Reporter(models.Model): first_name = models.CharField(maxlength=30) last_name = models.CharField(maxlength=30) email = models.EmailField() def __str__(self): return "%s %s" % (self.first_name, self.last_name) class Admin: pass class Article(models.Model): headline = models.CharField(maxlength=100) pub_date = models.DateField() reporter = models.ForeignKey(Reporter) def __str__(self): return self.headline class Meta: ordering = ('headline',) class Admin: pass
template:
<form action="" method="post"> {{ form.as_table }} <input type="submit" value="Submit" /> </form>
views.py:
from erpy.net.models import Article def article_add(request): ArticleForm = forms.form_for_model(Article) if request.method == "POST": form = ArticleForm(request.POST) if form.is_valid(): article_new = form.save() HttpResponseRedirect('/') else: context = {'form': form, 'error_msg': "Your form has errors!"} else: context = {'form': ArticleForm()} return render_to_response('net/index.html', context, context_instance=RequestContext(request))
then traceback is:
Traceback (most recent call last): File "/Users/gandalf/django/django_src/django/core/handlers/base.py" in get_response 77. response = callback(request, *callback_args, **callback_kwargs) File "/Users/gandalf/django/erpy/erpy/../erpy/net/views.py" in article_add 17. article_new = form.save() File "/Users/gandalf/django/django_src/django/newforms/models.py" in model_save 18. obj = self._model(**self.clean_data) File "/Users/gandalf/django/django_src/django/db/models/base.py" in __init__ 113. raise TypeError, "Invalid value: %r should be a %s instance, not a %s" % (f.name, f.rel.to, type(rel_obj)) TypeError at /net/ Invalid value: 'reporter' should be a <class 'erpy.net.models.Reporter'> instance, not a <type 'unicode'>
and POST is:
Variable Value
headline:'This is a test'
pub_date: '2005-7-27'
reporter: '1'
comment:3 by , 18 years ago
Thanks!
For a small testcase, use the models as above, and then:
from django import newforms as forms ArticleForm = forms.form_for_model(models.Article) post = {'headline':'This is a test', 'pub_date':'2005-7-27', 'reporter':'1'} form = ArticleForm(post) form.is_valid() form.save()
traceback as above.
I'm looking whether the patch in #3257 solves this.
Hi Jure, can you please attach a small model and a sequence of statements that demonstrates the bug?