0

Lots of documentation on this already, but haven't been able to get any of them to work for me. Like the title implies, trying to get a set of objects that use Django Voting to sort based on the number of their votes (high to low). Have tried this and a few others, but nothing fruitful.

Using Django Voting, here is 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)),

Questions in the URLconf leads to the questions view which renders 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 %}

Here's my current view:

#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))

I know I can't sort using "score" because it's not in the model. What do I need to change in my view to sort this correctly?

EDIT:

Robert, here's models.py. Tried your solution, and a number of variations, but I don't have a voting attribute to the model. Take a look:

#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)

Any insight?

Community
  • 1
  • 1
Chris Yin
  • 761
  • 1
  • 11
  • 23

1 Answers1

1

It'd be handy if you posted your model.py, but I'm going to make some guesses.

Firstly, this might help:

#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')
    ...

(don't need to use reverse, can just begin it with -)

I'm going to guess that your score could be sorted as follows:

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

The __ (double underscore) will refer to an attribute of related model.

I've been known to live and die by this: https://docs.djangoproject.com/en/dev/topics/db/queries/#related-objects

Williams
  • 4,044
  • 1
  • 37
  • 53
  • Though looking again if you're mad-keen on using reverse the following will also work: `k = Question.objects.filter(movie=p).order_by('q_pub_date').reverse()` (python is so awesome). – Williams Nov 09 '12 at 11:53
  • awesome, thanks for the reverse tip. ordering by votes still not working though, vote is not an actual attribute of the model, using django voting and didn't need to add a voting.integerfield() to get votes to work. this is why im not sure how to order it by votes, b/c it's not an actual attribute. edited my question to include the model. lmk if you need anything else, thanks in advance! – Chris Yin Nov 09 '12 at 18:33