Opened 8 years ago

Closed 7 years ago

#4519 closed (fixed)

Cannot use form_from_instance with a ManyToMany field

Reported by: todd.kennedy@… Owned by: msaelices
Component: Forms Version: master
Severity: Keywords: newforms manytomany sprintsept14
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

There seems to be an issue when trying to use form_from_instance if the model you're basing it on has a ManyToMany field. Here's the code I'm using and the python interpreter issue. I'm using SVN revision 5448.

class Ringback(models.Model):                                               
    content_provider = models.ForeignKey(ContentProvider)
    song_title = models.CharField(maxlength=255, db_index=True)
    artist = models.ForeignKey(Artist)
    wav_filename = models.CharField(maxlength=255)
    external_id = models.CharField(maxlength=255)
    price_code = models.CharField(maxlength=255)
    upload_to_comverse = models.BooleanField(default=True)
    upload_to_nms = models.BooleanField(default=True)
    comverse_categories = models.ManyToManyField(ComverseCategory, blank=True)
    nms_categories = models.ManyToManyField(NMSCategory, blank=True)
    reason= models.TextField(blank=True)
    entered_on = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)
    entered_by = models.ForeignKey(User)
    duplicate_entry = models.BooleanField(default=False)
    duplicate_reason = models.TextField(blank=True)
    exported_to = models.ManyToManyField(Carrier)
    import_set = models.ForeignKey(RBTSource)                
    exports = models.ManyToManyField(Export)      


>>> rb = Ringback.objects.get(pk=2)
>>> f = form_for_instance(rb, fields=('content_provider', 'song_title','artist','wav_filename','external_id','price_code'))
>>> print f()
[output truncated]
>>> f = form_for_instance(rb, fields=('content_provider', 'song_title','artist','wav_filename','external_id','price_code','comverse_categories'))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/newforms/models.py", line 106, in form_for_instance
    formfield = formfield_callback(f, initial=current_value)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/newforms/models.py", line 86, in <lambda>
    def form_for_instance(instance, form=BaseForm, fields=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/fields/related.py", line 746, in formfield
    defaults['initial'] = [i._get_pk_val() for i in defaults['initial']]
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/query.py", line 108, in __iter__
    return iter(self._get_data())
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/query.py", line 470, in _get_data
    self._result_cache = list(self.iterator())
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/query.py", line 174, in iterator
    select, sql, params = self._get_sql_clause()
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/query.py", line 484, in _get_sql_clause
    joins2, where2, params2 = self._filters.get_sql(opts)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/query.py", line 662, in get_sql
    joins2, where2, params2 = val.get_sql(opts)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/query.py", line 713, in get_sql
    return parse_lookup(self.kwargs.items(), opts)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/query.py", line 847, in parse_lookup
    joins2, where2, params2 = lookup_inner(path, lookup_type, value, opts, opts.db_table, None)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/query.py", line 965, in lookup_inner
    raise TypeError, "Cannot resolve keyword '%s' into field. Choices are: %s" % (name, ", ".join(choices))
TypeError: Cannot resolve keyword 'ringback' into field. Choices are: id, name, category_type, entered_on, updated_on, entered_by


Change History (7)

comment:1 Changed 7 years ago by msaelices

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to msaelices
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 7 years ago by msaelices

  • Keywords sprintsept14 added
  • Resolution set to wontfix
  • Status changed from assigned to closed

I cannot reproduce the error. I updated to revision 5448 and used this model (like ticket model):

from django.db import models
from django.contrib.auth.models import User


class ContentProvider(models.Model):
    name = models.CharField(maxlength=100)
    class Admin:pass

    def __str__(self):
        return self.name


class ComverseCategory(models.Model):
    name = models.CharField(maxlength=100)
    class Admin:pass

    def __str__(self):
        return self.name


class NMSCategory(models.Model):
    name = models.CharField(maxlength=100)
    class Admin:pass

    def __str__(self):
        return self.name


class Carrier(models.Model):
    name = models.CharField(maxlength=100)
    class Admin:pass

    def __str__(self):
        return self.name


class RBTSource(models.Model):
    name = models.CharField(maxlength=100)
    class Admin:pass

    def __str__(self):
        return self.name


class Export(models.Model):
    name = models.CharField(maxlength=100)
    class Admin:pass

    def __str__(self):
        return self.name


class Artist(models.Model):
    title = models.CharField(maxlength=100)
    class Admin:pass

    def __str__(self):
        return self.title

class Ringback(models.Model):
    content_provider = models.ForeignKey(ContentProvider)
    song_title = models.CharField(maxlength=255, db_index=True)
    artist = models.ForeignKey(Artist)
    wav_filename = models.CharField(maxlength=255)
    external_id = models.CharField(maxlength=255)
    price_code = models.CharField(maxlength=255)
    upload_to_comverse = models.BooleanField(default=True)
    upload_to_nms = models.BooleanField(default=True)
    comverse_categories = models.ManyToManyField(ComverseCategory, blank=True)
    nms_categories = models.ManyToManyField(NMSCategory, blank=True)
    reason= models.TextField(blank=True)
    entered_on = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)
    entered_by = models.ForeignKey(User)
    duplicate_entry = models.BooleanField(default=False)
    duplicate_reason = models.TextField(blank=True)
    exported_to = models.ManyToManyField(Carrier)
    import_set = models.ForeignKey(RBTSource)
    exports = models.ManyToManyField(Export)

    class Admin:pass

    def __str__(self):
        return self.song_title

And I get next results in python interpreter:

>>> rb = Ringback.objects.get(pk=2)
>>> f = forms.form_for_instance(rb, fields=('content_provider', 'song_title','artist','wav_filename','external_id','price_code','comverse_categories'))
>>> print f()
<tr><th><label for="id_content_provider">Content provider:</label></th><td><select name="content_provider" id="id_content_provider">
<option value="">---------</option>
<option value="1" selected="selected">A provider</option>
[ truncated ]

comment:3 Changed 7 years ago by msaelices

  • Resolution wontfix deleted
  • Status changed from closed to reopened

comment:4 Changed 7 years ago by msaelices

  • Triage Stage changed from Unreviewed to Accepted

comment:5 Changed 7 years ago by msaelices

  • Triage Stage changed from Accepted to Unreviewed

could you put a better fail test?

comment:6 Changed 7 years ago by Øyvind Saltvik <oyvind@…>

Was this not fixed in [5919] ?

comment:7 Changed 7 years ago by Øyvind Saltvik <oyvind@…>

  • Resolution set to fixed
  • Status changed from reopened to closed

Fixed in [5919] if you can reproduce the bug in django SVN, let us know.

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