Solution 1 :

Within your admin, you can use the ordering attribute like so…

from django.contrib import admin

from .models import Vote

class VoteAdmin(admin.ModelAdmin):
    ordering = ('count',)

admin.site.register(Vote, VoteAdmin)

Problem :

so i made it that my books will show in my admin but i dont know how to order the books(in the admin) by votes and not by last voted. I found some answers here on overflow but i wasnt able to integrate them by myself. Here are my files:

admin.py

from django.contrib import admin

from .models import Vote

admin.site.register(Vote)

apps.py

from django.apps import AppConfig

class SurveyConfig(AppConfig):

    name = 'survey'

forms.py

from django import forms

from .models import Vote

class VotingForm(forms.Form):

    chosen_books_options = forms.MultipleChoiceField(choices=[], label='Book Name', required=False,

                                                     widget=forms.SelectMultiple(

                                                        attrs={

                                                             'class': 'form-control'
                                                         }
                                                     ))
    other_book_name = forms.CharField(label='Other', max_length=100, required=False,
                                      widget=forms.TextInput(
                                        attrs={
                                              'class': 'form-control',
                                              'placeholder': 'Did we miss something?'
                                          }
                                      ))
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        unique_books_names = Vote.objects.order_by('book_name').values_list('book_name', flat=True).distinct()
        self.fields['chosen_books_options'].choices = [(book_name, book_name) for book_name in unique_books_names]

models.py

from django.db import models, transaction

class Vote(models.Model):

    book_name = models.CharField(max_length=200)

    count = models.IntegerField(default=0)
    def __str__(self):

        return '%s: %d votes' % (self.book_name, self.count)

    @classmethod

    def bulk_vote(cls, book_names):
        with transaction.atomic():
            for book_name in book_names:
                if len(book_name) == 0:
                    continue
                if Vote.objects.filter(book_name=book_name).exists():

                    Vote.objects.filter(book_name=book_name).update(count=models.F('count') + 1)
                else:
                    Vote.objects.create(book_name=book_name, count=1)

views.py

from django.shortcuts import render

from .forms import VotingForm

from .models import Vote

def index(request):
    if request.method == 'POST':
        form = VotingForm(request.POST)
        if form.is_valid():
            chosen_books_options = form.cleaned_data.get('chosen_books_options', [])
            other_book_name = form.cleaned_data.get('other_book_name', '')
            Vote.bulk_vote(chosen_books_options + [other_book_name])
        message = 'Thank You For Your Contribution!'
    elif request.method == 'GET':
        message = ''
    form = VotingForm()
    return render(request, 'templates/survey.html', {'form': form, 'message': message})

html

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
    <title>Document</title>
    <style>       
        .form-control{
          width: 50%;
        }

    </style>

</head>

<body>
  <div class="container" id="thisone">
    <h3>Select which books you'd like us to get started with.</h3>

    <h5>{{ message }}</h5>
    <form action="" method="post">

        {% csrf_token %}

        {{ form.as_p }}

        <button type="submit" class="btn btn-primary">Submit</button>

    </form>

  </div>

</body>

thank you for reading
note: this is not fully my code

Comments

Comment posted by Boritobison

Thank you for your answer! Sadly nothing has changed when i put the code in….

Comment posted by Wayne Lambert

Sorry, you’ve called your field

Comment posted by Boritobison

I also tried that one before your new solution. It isnt working either… I dont have to migrate right? If yes there are “no changes detected”… Still a big thanks to you!

Comment posted by Wayne Lambert

Might be a silly question but have you also made your migrations before trying to migrate?

Comment posted by Wayne Lambert

OK, it’s missing the VoteAdmin on the last line. I’ll change the answer and hopefully that will sort the issue!

By