Django order_by(';?';)[:n]n使用我从数据库中获取的变量?但不起作用
TypeError:“>=”在“guestionNum”和“int”的实例之间不受支持 如何使用从数据库中获取的变量n生成切片Django order_by(';?';)[:n]n使用我从数据库中获取的变量?但不起作用,django,python-3.x,sql-order-by,Django,Python 3.x,Sql Order By,TypeError:“>=”在“guestionNum”和“int”的实例之间不受支持 如何使用从数据库中获取的变量n生成切片 def home (request): questions = Article.objects.order_by('?') n = guestionNum.objects.all() n = n[0] answers = Answer.objects.all().order_by('?')[:n] return render_to_response('questi
def home (request):
questions = Article.objects.order_by('?')
n = guestionNum.objects.all()
n = n[0]
answers = Answer.objects.all().order_by('?')[:n]
return render_to_response('question/home.html', {'questions': questions, 'answers': answers,'n':n, 'username':auth.get_user(request).username}) `
您不能切片,因为上面的行返回的是一个实例而不是int对象
试一试
然后把它传给切片
n = guestionNum.objects.all()
n=n[0].fieldname
现在,n
的类型是instance not queryset
因此,您可以使用下面代码中的直接n值
n = guestionNum.objects.all()
n = n[0]
# n == guestionNum.objects.first() // Both have same type and data
我该怎么做?n从模型中获取值:{class guestionNum(models.model):question_num=models.CharField(max_length=3)def__str___(self):返回self.question_num}尝试len(n)和print(n)检查返回的内容?那么,从模型中获取值是什么意思呢?在db集合5中…和print 5TypeError异常值:类型为'guestionNum'的对象没有len()print n=1,但在db集合编号5а中,我想对变量进行切片,该变量是数据库中的索引,等于5
n = guestionNum.objects.all()
n = n[0]
# n == guestionNum.objects.first() // Both have same type and data
n = guestionNum.objects.first().fieldname