我正在尝试为某机构中基于Web的课程计划应用程序的数据库记录。我担心的是,由于在views.py中的关系M2M而无法在中间表中进行记录,但是它在浏览器的shell中有效我有一个 ValueError:用于精确查找的QuerySet值必须限于使用切片的一种结果。
回溯切换到复制和粘贴视图
C:\Program Files\Python37\lib\site-packages\django\core\handlers\exception.py in inner
response = get_response(request)
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\core\handlers\base.py in _get_response
response = self.process_exception_by_middleware(e, request)
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\core\handlers\base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
...
▶ Local vars
E:\PLACORESA\configuration\views.py in departemnent
cursus = get_object_or_404(Cursus, libelle_cursus=cursus )
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\shortcuts.py in get_object_or_404
return queryset.get(*args, **kwargs)
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\db\models\query.py in get
num = len(clone)
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\db\models\query.py in __len__
self._fetch_all()
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\db\models\query.py in _fetch_all
self._result_cache = list(self._iterable_class(self))
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\db\models\query.py in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\db\models\sql\compiler.py in execute_sql
sql, params = self.as_sql()
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\db\models\sql\compiler.py in as_sql
where, w_params = self.compile(self.where) if self.where is not None else ("", [])
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\db\models\sql\compiler.py in compile
sql, params = node.as_sql(self, self.connection)
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\db\models\sql\where.py in as_sql
sql, params = compiler.compile(child)
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\db\models\sql\compiler.py in compile
sql, params = node.as_sql(self, self.connection)
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\db\models\lookups.py in as_sql
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
...
▶ Local vars
C:\Program Files\Python37\lib\site-packages\django\db\models\lookups.py in process_rhs
'The QuerySet value for an exact lookup must be limited to '
...
▶ Local vars
models.py
class Departement(models.Model):
code_departement=models.CharField("code du département", max_length=100, unique=True)
libelle_departement=models.CharField("Libellé du département", max_length=100)
faculte=models.ForeignKey("Faculte", on_delete=models.CASCADE)
cursus=models.ManyToManyField("Cursus", through="AvoirCursus")
class Cursus(models.Model):
code_cursus=models.CharField("Code du cursus", max_length=10, unique=True)
libelle_cursus=models.CharField("Libellé du cursus", max_length=100)
class AvoirCursus(models.Model):
cursus=models.ForeignKey("Cursus", on_delete=models.CASCADE)
departement=models.ForeignKey("Departement", on_delete=models.CASCADE)
views.py
if request.method == 'POST':
f = forms.Departement_Form(request.POST)
if f.is_valid():
dept = f.save(commit=False)
code_departement = f.cleaned_data['code_departement'].upper()
dept.code_departement = code_departement
cursus = f.cleaned_data['cursus']
dept.save()
cursus = get_object_or_404(Cursus, libelle_cursus=cursus )
AvoirCursus(departement=dept, cursus=cursus)
return redirect('configuration:lister_departement')
答案 0 :(得分:0)
您确定诅咒对象的获取正确吗? 也许表单的诅咒会返回多个值或字符串?
此外,尝试在get_or_404之后添加print(cursus)并查看内部内容。
您到底想在这里做什么?
AvoirCursus(部门=部门,cursus = cursus)
确定不应该吗?
AvoirCursus.objects.create(departement = dept,cursus = cursus)
?
没有堆栈跟踪就很难弄清楚。
答案 1 :(得分:0)
我已经成功解决了我的问题。视图中使用的表单是来自models.py的通用表单。当我打印(cursus)时,结果是一个包含选定项目的查询集。要编写中间数据库,我进行了“ cursus”过程以拥有不同的对象。
**the views.py is:**
if f.is_valid():
dept = f.save(commit=False)
code_departement = f.cleaned_data['code_departement']
dept.code_departement = code_departement
print(code_departement)
cursus = f.cleaned_data['cursus']
dept.save()
for i in cursus:
AvoirCursus.objects.create(departement=dept, cursus=i)