首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查询集在django shell中不为空,但在函数中为空

查询集在django shell中不为空,但在函数中为空
EN

Stack Overflow用户
提问于 2019-05-11 12:06:57
回答 1查看 67关注 0票数 0

我的views.py中有以下函数:

代码语言:javascript
复制
def edit_theorem(theorem):
  print(type(theorem))
  print(theorem.id)
  old_list = theorem.included_elements.all()
  print(old_list)
  ...

这些打印函数的输出为:

代码语言:javascript
复制
<class 'app.models.Theorem'>
65
<QuerySet []>

但是,当我运行python manage.py shellfrom app.models import *t=Theorem.objects.get(id=65)print(t.included_elements.all())时,它打印一个非空的查询集。

为什么?

我的models.py看起来像这样:

代码语言:javascript
复制
class Element(models.Model):
  included_elements = models.ManyToManyField('Element', through='IncludedElements')
  ...

class IncludedElements(models.Model):
  ...

def Theorem(Element):
  ...

DB是PostgreSQL。

EN

回答 1

Stack Overflow用户

发布于 2019-05-11 16:27:25

在调用函数edit_theorm之前,我不确定您在做什么,但我认为这就是问题所在

代码语言:javascript
复制
def edit_theorem(theorem_id):
  theorem = Theorem.objects.get(id=theorem_id)
  old_list = theorem.included_elements.all()
  print(old_list)

def edit_theorem(request, id):
  //action within the view

  edit_theorem(id)

  //other action within the view
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56087100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档