我试图通过转换为列表对通过表单输入的字符串进行排序,但是却收到对象不可迭代的错误。没有内置方法如何排序?
models.py
class LinkedList(models.Model):
post = models.TextField(max_length=256)
def __str__(self):
l1=list(self.post.split(" "))
return str(l1)
views.py
def listform(request):
form = LLForm(request.POST)
if request.method == 'POST':
if form.is_valid():
form.save(commit=True)
list1 = LinkedList.objects.all().last()
listll=list(list1)
for i in range(1, len(listll)):
key = listll[i]
j = i-1
while j >= 0 and key < listll[j] :
listll[j + 1] = listll[j]
j -= 1
listll[j + 1] = key
cont={'ll': listll}
return render(request,'basicapp/linked.html',context=cont)
else:
form = LLForm()
return render(request,'basicapp/listform.html',{'form':form})
答案 0 :(得分:0)
您的查询集似乎不正确。
您需要按以下方式更改查询集:
from django.db.models.functions import Length
qs = LinkedList.objects.annotate(post_len=Length('post')).filter(post_len__gte=2)
此queryset
将为您提供所需的内容。
您可以订购,但可以:
my_list = list(qs)
for l in my_list:
# Do Comparison Here
# l will be LinkedList instance.