2012-11-09 121 views
0

很多關於此的文件,但一直沒有能夠讓他們爲我工作。就像標題所暗示的那樣,試圖獲得一組使用Django投票的對象,根據它們的投票數量(從高到低)進行排序。已嘗試this和其他一些,但沒有結果。Django投票:按投票排序

使用Django投票,這裏是URL的conf & HTML

#urls.py 
url(r'^$', 'questions'), 
url(r'^(?P<object_id>\d+)/(?P<direction>up|down|clear)/vote/$', 
     vote_on_object, dict(model = Question, template_object_name = 'question', 
     template_name = 'qanda/confirm_vote.html', post_vote_redirect = '/home/', allow_xmlhttprequest=True)), 

問題URL配置導致的問題查看這使得questions.html

#questions.html 
{% load voting_tags %} 
    {% votes_by_user user on the_question as vote_dict %} 
    {% scores_for_objects the_question as score_dict %} 
     <div class="question"> 
      {% if the_question %} 
     <ul> 
      {% for question in the_question %} 
       {% dict_entry_for_item question from vote_dict as vote %} 
       {% dict_entry_for_item question from score_dict as score %} 
      <div class="votearrowdiv"> 
      <div class="upvotearrow"></div></a> 
      <form class="linkvote" id="linkup{{ question.id }}" action="/home/{{ question.id }}/{% if vote and vote.is_upvote %}clear{% else %}up{% endif %}/vote/" method="POST"> 
       {% csrf_token %} 
       <input type="image" id="linkuparrow{{ question.id }}" src="{{ media_url }}img/aup{% if vote and vote.is_upvote %}mod{% else %}grey{% endif %}.png"> 
      </form> 

      <div class="downvotearrow"></div></a> 
      <form class="linkvote" id="linkdown{{ question.id }}" action="/home/{{ question.id }}/{% if vote and vote.is_downvote %}clear{% else %}down{% endif %}/vote/" method="POST"> 
       {% csrf_token %} 
       <input type="image" id="linkdownarrow{{ question.id }}" src="{{ media_url }}img/adown{% if vote and vote.is_downvote %}mod{% else %}grey{% endif %}.png"> 
      </form> 
      </div> 
      <li> 
       <div class="votecounter"><div class="numbercount"> 
        <span class="score" id="linkscore{{ question_id }}" 
        title="after {{ score.num_votes|default:0 }} vote{{ score.num_votes|default:0|pluralize }}"> 
        {{ score.score|default:0 }} 
        </span> 
       </div></div> 
       <a href="/home/{{ movie.id }}/{{ question.id }}/">{{ question.question_text }}</a> 
     {% endfor %} 
{% endif %} 

這裏是我當前視圖:

#views.py 
def questions(request, movie_id): 
    p = Movie.objects.get(pk=movie_id) 
    k = Question.objects.filter(movie=p).order_by('q_pub_date') 
    l = k.reverse() 
    return render_to_response('qanda/questions.html', {'movie':p, 'the_question':l}, context_instance = RequestContext(request)) 

我知道我不能使用「分數」排序,因爲它不在m中奧德爾。在我看來,爲了正確排序,我需要改變什麼?

編輯:

羅伯特,這裏的models.py。嘗試過你的解決方案和一些變化,但我沒有投票屬性的模型。看看:

#models.py 
class Question(models.Model): 
    movie   = models.ForeignKey(Movie, blank=True, null=True) 
    question_text = models.CharField(verbose_name = "Question", max_length = 250) 
    question_detail = models.CharField(verbose_name = "Details (Optional)", max_length = 5000, blank = True, null = True) 
    q_pub_date  = models.DateTimeField(auto_now_add = True) 
    q_author  = models.ForeignKey(User) 

任何見識?

回答

1

如果您發佈您的model.py會很方便,但我會做一些猜測。

首先,這可能幫助:

#views.py 
def questions(request, movie_id): 
    p = Movie.objects.get(pk=movie_id) 
    k = Question.objects.filter(movie=p).order_by('-q_pub_date') 
    ... 

(不需要使用反接,可只是-開始吧)

我要去猜測,你的分數可能被歸類爲如下:

k = Question.objects.filter(movie=p).order_by('movie__score', '-q_pub_date') 

__(雙下劃線)將參考相關模型的屬性。

我已經知道生活和這個死:https://docs.djangoproject.com/en/dev/topics/db/queries/#related-objects

+0

雖然再次找如果你在使用逆下面還將努力瘋敏銳:'K = Question.objects.filter(電影= p).order_by('q_pub_date')。reverse()'(python太棒了)。 – Williams

+0

真棒,謝謝你的反向提示。通過投票排序仍然不工作,投票不是模型的實際屬性,使用Django投票,並不需要添加voting.integerfield()來獲得投票。這就是爲什麼我不知道如何通過投票來排序它,B/C它不是一個實際的屬性。編輯我的問題,包括模型。如果您需要其他任何東西,請提前致謝! –