Opened 10 years ago

Closed 10 years ago

#3342 closed (duplicate)

ForeignKey relation doesn't work with newforms form_for_model() save

Reported by: Jure Cuhalev <gandalf@…> Owned by: Adrian Holovaty
Component: Forms Version: master
Severity: Keywords: ForeignKey newforms
Cc: gandalf@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


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

comment:1 Changed 10 years ago by mir@…

Hi Jure, can you please attach a small model and a sequence of statements that demonstrates the bug?

comment:2 in reply to:  1 Changed 10 years ago by Jure Cuhalev <gandalf@…> (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:

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:


<form action="" method="post">
 {{ form.as_table }}
 <input type="submit" value="Submit" />

from 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 =
          context = {'form': form, 
                     'error_msg': "Your form has errors!"}
        context = {'form': ArticleForm()}

    return render_to_response('net/index.html', context,

then traceback is:

Traceback (most recent call last):
File "/Users/gandalf/django/django_src/django/core/handlers/" in get_response
  77. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/gandalf/django/erpy/erpy/../erpy/net/" in article_add
  17. article_new =
File "/Users/gandalf/django/django_src/django/newforms/" in model_save
  18. obj = self._model(**self.clean_data)
File "/Users/gandalf/django/django_src/django/db/models/" in __init__
  113. raise TypeError, "Invalid value: %r should be a %s instance, not a %s" % (,, type(rel_obj))

  TypeError at /net/
  Invalid value: 'reporter' should be a <class ''> instance, not a <type 'unicode'>

and POST is:

Variable Value
headline:'This is a test'
pub_date: '2005-7-27'
reporter: '1'

comment:3 Changed 10 years ago by Michael Radziej <mir@…>

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)

traceback as above.

I'm looking whether the patch in #3257 solves this.

comment:4 Changed 10 years ago by Michael Radziej <mir@…>

Resolution: duplicate
Status: newclosed

duplicate of #3257

Note: See TracTickets for help on using tickets.
Back to Top