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
<https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method>
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
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/b211f94e-5927-404c-a35a-f6020acfc73c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.