TypeError : Unable to save m2m relation modelform

393 views
Skip to first unread message

kevin

unread,
Aug 14, 2014, 3:22:11 PM8/14/14
to django...@googlegroups.com

Type Error : 'ExtendedTag' instance expected, got <BasicTag: Hello>


I had created m2m field (required false). When I tried to save form with parent select (from multiple choice field), It rises type error. When I tried to save it without selecting parent and category, it work fine. I couldn't able to figure out what I am missing. 
Code Snipped (full available at https://github.com/Pattinson/m2mdemo)

models.py

class BasicTag(models.Model):
    name = models.CharField(max_length=150, verbose_name="Tag Name")
    image_class = models.CharField(max_length=30, blank=True)
    color = models.CharField(max_length=10, blank=True)

    def __unicode__(self):
        return self.name


class ExtendedTag(models.Model):
    parent = models.ManyToManyField(BasicTag, blank=True,
                                    related_name="parent")
    category = models.ManyToManyField(BasicTag, blank=True,
                                      related_name="category")

    def __unicode__(self):
        return self._id


class CombineTag(BasicTag, ExtendedTag):
    """

    """

forms.py

class CombineTagForm(forms.ModelForm):
    class Meta:
        model = CombineTag

I had follow official doc which say that

If your model has a many-to-many relation and you specify commit=False when you save a form, Django cannot immediately save the form data for the many-to-many relation. This is because it isn’t possible to save many-to-many data for an instance until the instance exists in the database.

Errors

Request Method: POST
Request URL: http://127.0.0.1:8080/
Django Version: 1.6.5
Exception Type: TypeError
Exception Value:

'ExtendedTag' instance expected, got 
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py in _add_items, line 649
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:

['/home/virus/project/m2mdemo',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
Server time: Thu, 14 Aug 2014 18:37:29 +0000
Traceback Switch to copy-and-paste view

/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/home/virus/project/m2mdemo/tag/views.py in add
form.save_m2m() ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/forms/models.py in save_m2m
f.save_form_data(instance, cleaned_data[f.name]) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py in save_form_data
setattr(instance, self.attname, data) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py in set
manager.add(*value) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py in add
self._add_items(self.source_field_name, self.target_field_name, *objs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py in _add_items
raise TypeError("'%s' instance expected, got %r" % (self.model._meta.object_name, obj)) ...
▶ Local vars

Collin Anderson

unread,
Aug 14, 2014, 5:03:26 PM8/14/14
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages