When saving ManyToMany Field value in django ,error occures invalid literal for int() with base 10
<p>I am trying to save ManyToMany filed value in django model objects.But when i ma saving an error comes invalid literal for int() with base 10.
My code is</p>

<pre><code>def saveDetail(request):

    userExp = str(request.GET.get('user'))
    tags = request.POST.getlist('tags')
    comment = request.POST.get('comment')
    exp = customer.objects.get(user = userExp)   
    exp.tags = tags
    exp.save()
</code></pre>

<p>and the error is </p>

<pre><code>ValueError: invalid literal for int() with base 10: 'tag2 
</code></pre>

<p>my tagExp model is</p>

<pre><code>class TagsExp(models.Model):


    label=models.CharField(max_length=50,null=True, blank=True)


    def __unicode__(self):
        return str(self.label)
</code></pre>

<p>My customer model is</p>

<pre><code>class Customer(models.Model):

    user = models.OneToOneField(User)    remark = models.TextField(null=True, blank=True)
    tags = models.ManyToManyField(TagsExp, null=True, blank=True)
    time = models.DateField(null=True)
</code></pre>

<p>I have added traceback of my error below.</p>

<p>Traceback:</p>

<pre><code>File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/channels/handler.py" in process_exception_by_middleware
  227.             return super(AsgiHandler, self).process_exception_by_middleware(exception, request)

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/home/cp/Documents/projects/beta/upKonnect/decorators.py" in wrapped
  12.             return view_func(request, *args, **kwargs)

File "/home/cp/Documents/projects/beta/adminProfile/views.py" in saveExpDetail
  506.     exp.tags = tags

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py" in __set__
  481.         manager.set(value)

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py" in set
  910.                     self.add(*new_objs)

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py" in add
  843.                 self._add_items(self.source_field_name, self.target_field_name, *objs)

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py" in _add_items
  986.                             '%s__in' % target_field_name: new_ids,

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/query.py" in filter
  790.         return self._filter_or_exclude(False, *args, **kwargs)

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
  808.             clone.query.add_q(Q(*args, **kwargs))

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
  1243.         clause, _ = self._add_q(q_object, self.used_aliases)

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q
  1269.                     allow_joins=allow_joins, split_subq=split_subq,

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_filter
  1199.             condition = lookup_class(lhs, value)

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/lookups.py" in __init__
  19.         self.rhs = self.get_prep_lookup()

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/fields/related_lookups.py" in get_prep_lookup
  54.                     self.lookup_name, self.rhs)

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
  746.             return [self.get_prep_value(v) for v in value]

File "/home/cp/Documents/beta/envBeta/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
  976.         return int(value)

Exception Type: ValueError at /upadmin/save-Exp-Detail/
Exception Value: invalid literal for int() with base 10: 'tag2 '
</code></pre>

<p><code>tags</code> is a <code>ManyToMany</code> field. You can't use a direct assignment to a list item to update the field. You should instead assign via the field's <code>add</code> method tag <em>objects</em> corresponding to the items in the list.</p>

<p>Assuming you <code>TagsExp</code> has a field <code>label</code> which corresponds to the items in the list, you would do:</p>

<pre><code>for tag_label in tags:
    tag_label = tag_label.strip().lower() # clean up tag
    tag, _ = TagsExp.objects.get_or_create(label=tag_label)
    exp.tags.add(tag)
exp.save()
</code></pre>

<hr>

<p>On another note, the tag in your traceback has a trailing space. That would create a new tag if there were no clean up as opposed to getting the existing one. Also, multiple cases will not be handled by default. I think you should give a look to <a href="https://django-taggit.readthedocs.io/en/latest/" rel="nofollow"><code>django-taggit</code></a> app for <em>tagging</em> which has great features for managing whitespace characters and different cases.</p>

python django