我正在尝试在django模型对象中保存ManyToMany字段值。但是当我保存错误时,带有基数10的int()的文字无效。 我的代码是
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()
,错误是
ValueError: invalid literal for int() with base 10: 'tag2
我的tagExp模型是
class TagsExp(models.Model):
label=models.CharField(max_length=50,null=True, blank=True)
def __unicode__(self):
return str(self.label)
我的客户模式是
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)
我在下面添加了我的错误追溯。
回溯:
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 '
答案 0 :(得分:1)
tags
是ManyToMany
字段。您不能使用直接分配到列表项来更新字段。您应该通过与列表中的项目对应的字段add
方法标记对象进行分配。
假设您TagsExp
的字段label
与列表中的项目相对应,您可以执行以下操作:
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()
另一方面,traceback中的标记有一个尾随空格。如果没有清理而不是获取现有标签,那将创建一个新标签。此外,默认情况下不会处理多个案例。我认为您应该查看django-taggit
应用标记,它具有管理空白字符和不同案例的强大功能。