我的views.py中有以下函数:
def edit_theorem(theorem):
print(type(theorem))
print(theorem.id)
old_list = theorem.included_elements.all()
print(old_list)
...这些打印函数的输出为:
<class 'app.models.Theorem'>
65
<QuerySet []>但是,当我运行python manage.py shell、from app.models import *、t=Theorem.objects.get(id=65)、print(t.included_elements.all())时,它打印一个非空的查询集。
为什么?
我的models.py看起来像这样:
class Element(models.Model):
included_elements = models.ManyToManyField('Element', through='IncludedElements')
...
class IncludedElements(models.Model):
...
def Theorem(Element):
...DB是PostgreSQL。
发布于 2019-05-11 16:27:25
在调用函数edit_theorm之前,我不确定您在做什么,但我认为这就是问题所在
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 viewhttps://stackoverflow.com/questions/56087100
复制相似问题