Code

Opened 7 years ago

Closed 7 years ago

#3342 closed (duplicate)

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

Reported by: Jure Cuhalev <gandalf@…> Owned by: adrian
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:

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 (0)

Change History (4)

comment:1 follow-up: Changed 7 years ago by mir@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 7 years ago by Jure Cuhalev <gandalf@…>

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 Changed 7 years ago by Michael Radziej <mir@…>

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.

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

  • Resolution set to duplicate
  • Status changed from new to closed

duplicate of #3257

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.