如何使用用户id创建字典?

2024-06-16 12:05:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个编程初学者,我有一个障碍,所以基本上我想创建这样的东西

set = {
        user_id_1 : 'result_user_id_1', 
        user_id_2 : 'result_user_id_2', 
        user_id_3 : 'result_user_id_3' 
      }

我希望每个用户在字典里都有自己的分数。你知道吗

结果来自mytags(teamplatetags),它是所有用户为了获得最终分数而相互打分的一个分数。你知道吗

你知道吗型号.py你知道吗

from django.db import models
from django.conf import settings


VALOARE = (
    (1, "Nota 1"),
    (2, "Nota 2"),
    (3, "Nota 3"),
    (4, "Nota 4"),
    (5, "Nota 5"),
    (6, "Nota 6"),
    (7, "Nota 7"),
    (8, "Nota 8"),
    (9, "Nota 9"),
    (10, "Nota 10"),
)


class Punctaj(models.Model):
    acordat_de = models.ForeignKey(settings.AUTH_USER_MODEL, default=0)
    acordat_catre = models.ForeignKey(settings.AUTH_USER_MODEL, default=0, related_name="acordat_catre")
    nota = models.PositiveSmallIntegerField(default=0, choices=VALOARE)

你知道吗视图.py你知道吗

def home(request):
    data = dict()
    data['users']=User.objects.all()

    if request.method == "POST":
        for key in request.POST:
            if 'nota_' in key:
                nota_acordata = Punctaj.objects.filter(acordat_de=request.user, acordat_catre__id=key.split('_')[1]).first()
                if nota_acordata:
                    nota_acordata.nota = request.POST.get(key)
                    nota_acordata.save()

                else:
                    Punctaj.objects.create(acordat_de=request.user, acordat_catre_id=key.split('_')[1], nota=request.POST.get(key))
                    messages.success(request,"Successfully Voted")

        return redirect('home')

    return render(request, "login/home.html", data)

你知道吗我的标签.py-模板标签

@register.simple_tag
def results(user):
    suma = Punctaj.objects.filter(acordat_catre=user).aggregate(punctaj=Sum('nota')).get("punctaj")
    count = Punctaj.objects.filter(acordat_catre=user).count()
    if not suma:
        result = 0
    else:
        result = int(suma)/count
    return result

模板

<form class ="nota" method="POST" action="">{% csrf_token %}
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th> User </th>
                <th> Nota finala </th>
            </tr>
        </thead>

        <tbody>
        {% for fotbalist in users %}
            <tr>
                <td>{{ fotbalist.username }}</td>
                <td>
                    <p>{% results fotbalist %}</p>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</form>

Tags: keyidifobjectsmodelsrequesttableresult
1条回答
网友
1楼 · 发布于 2024-06-16 12:05:07

您正在寻找^{},看起来您只是想要每个用户的平均“nota”?你知道吗

User.objects.annotate(score=Avg('acordat_catre__nota'))

结果的用户列表现在将有一个属性“score”,与模板标签相比,这个属性的一个优点是它将大大减少查询的数量

你的模板现在是

    {% for fotbalist in users %}
        <tr>
            <td>{{ fotbalist.username }}</td>
            <td>
                <p>{{ fotbalist.score }}</p>
            </td>
        </tr>
    {% endfor %}

如果你真的只想要这本字典,你就得做

dict(User.objects.annotate(score=Avg('acordat_catre__nota')).values_list('id', 'score'))

相关问题 更多 >