Django

Code

Ticket #3342 (closed: duplicate)

Opened 2 years ago

Last modified 2 years ago

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

Reported by: Jure Cuhalev <gandalf@owca.info> Assigned to: adrian
Milestone: Component: Forms
Version: SVN Keywords: ForeignKey newforms
Cc: gandalf@owca.info Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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.

Attachments

Change History

(follow-up: ↓ 2 ) 01/21/07 11:54:48 changed by mir@noris.de

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

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

(in reply to: ↑ 1 ) 01/21/07 12:18:54 changed by Jure Cuhalev <gandalf@owca.info>

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'

01/23/07 03:04:05 changed by Michael Radziej <mir@noris.de>

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.

01/23/07 04:23:13 changed by Michael Radziej <mir@noris.de>

  • status changed from new to closed.
  • resolution set to duplicate.

duplicate of #3257


Add/Change #3342 (ForeignKey relation doesn't work with newforms form_for_model() save)




Change Properties
Action