在我之前的问题how-to-save-many-to-many-relationship-in-django帮助我解决了如何保存多个但我有一些问题需要保存。我们走了......
我有像
这样的forloop模板{% for items in store %}
<input type="checkbox" value="{{ items.id|safe }}" name="store[]">
{% endfor %
这里的任务是拯救许多关系。
所以
if request.method == 'POST':
...
new_track.save()
some_var = request.POST.getlist('store[]')
some_var
给我[u'2', u'4', u'3']
new_store = Store.objects.filter(id__in=some_var)
pprint.pprint(new_store)
new_track.store.add(new_store)
new_store给了我[<Store: Store object>, <Store: Store object>, <Store: Store object>]
。以前我尝试使用的objects.get()工作但保存多个我来使用过滤器,但不知道如何保存多个对象。现在我有错误
int() argument must be a string or a number, not 'QuerySet'
更新:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/dashboard/track_info/10/
Django Version: 1.7
Python Version: 2.7.6
Installed Applications:
('django.contrib.sites',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dsp',
'frontend',
'ckeditor',
'social.apps.django_app.default')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "/home/ri/studio/Audiotube/dsp/decorators.py" in inner
46. return view(request, *args, **kwargs)
File "/home/ri/studio/Audiotube/dsp/views.py" in track_infodetail
338. new_track.store.add(new_store)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in add
917. self._add_items(self.source_field_name, self.target_field_name, *objs)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in _add_items
1010. '%s__in' % target_field_name: new_ids,
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/query.py" in filter
691. return self._filter_or_exclude(False, *args, **kwargs)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
709. clone.query.add_q(Q(*args, **kwargs))
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
1287. clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q
1314. current_negated=current_negated, connector=connector)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_filter
1181. lookups, value)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in get_lookup_constraint
1550. root_constraint.add(lookup_class(Col(alias, targets[0], sources[0]), value), AND)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/lookups.py" in __init__
82. self.rhs = self.get_prep_lookup()
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/lookups.py" in get_prep_lookup
85. return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
648. return [self.get_prep_value(v) for v in value]
File "/home/ri/studio/env/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
915. return int(value)
Exception Type: TypeError at /dashboard/track_info/10/
Exception Value: int() argument must be a string or a number, not 'QuerySet'
答案 0 :(得分:2)
答案 1 :(得分:1)
new_store是queryset,而不是单个对象。使用get()方法只返回单个模型对象。
相反,如果您希望使用与get()相同的进程,则必须与查询集中的每个对象进行交互。因此,您可以使用for each_store in new_store: new_track.store.add(each_store)
(编辑以消除猜想;以上内容将起作用,但如果需要考虑,应该有更多性能友好的方法来实现这一点)
我怀疑该错误与Django试图为add函数传递单个对象的pk有关,在执行某些数据库操作之前将值准备为整数。期望一个整数来查找字段但是传递了queryset对象类型会吐出你看到的错误。