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
I cannot reproduce the error. I updated to revision 5448 and used this model (like ticket model):
And I get next results in python interpreter: