我有一個模型,有回覆,模板中回覆最高的應該是第一個。order_by在sqlite中正常工作,但不在postgres中
在我的本地機器上它正確地到達服務器回覆但是基於upvote字段的FK過濾。
型號:
class solution(models.Model):
doubt=models.ForeignKey(doubts,related_name='answers')
reply=models.TextField()
snapshot = models.FileField(upload_to='support',null=True,blank=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
user=models.ForeignKey(settings.AUTH_USER_MODEL,null=True,blank=True)
upvote = models.ManyToManyField(User,related_name='upvoteuser')
@property
def total_upvotes(self):
return self.upvote.count()
def __str__(self):
return str(self.doubt)
視圖
def ticketdetails(request,slug,pk):
ticketis=doubts.objects.get(pk=pk)
replies = ticketis.answers.all().order_by('-upvote')
if request.method== 'POST':
form=replyform(request.POST)
if form.is_valid():
new_form=form.save(commit=False)
new_form.user=request.user
new_form.doubt=ticketis
new_form.save()
form=replyform()
return HttpResponseRedirect(reverse('ticketdetail',kwargs={'slug':ticketis.slug,'pk':ticketis.id}),messages.add_message(request, messages.SUCCESS,'Response submitted succesfully.'))
else:
form=replyform()
return render(request,'ticketview.html',{'ticketis':ticketis,'replies':replies,'form':form})
upvote模型的什麼部分,你試圖命令?它是否有任何指定的順序? – Sayse
Upvote字段應該是第一個數字。 –