On utilise un modèle custom pour les votes

This commit is contained in:
Tom Hubrecht 2021-03-18 14:45:06 +01:00
parent ad790b4676
commit f782b7402d
2 changed files with 63 additions and 0 deletions

View file

@ -0,0 +1,57 @@
# Generated by Django 2.2.19 on 2021-03-18 13:41
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("elections", "0008_question_voters"),
]
operations = [
migrations.CreateModel(
name="Vote",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
],
),
migrations.RemoveField(
model_name="option",
name="voters",
),
migrations.AddField(
model_name="option",
name="voters",
field=models.ManyToManyField(
blank=True,
related_name="votes",
through="elections.Vote",
to=settings.AUTH_USER_MODEL,
),
),
migrations.AddField(
model_name="vote",
name="option",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="elections.Option"
),
),
migrations.AddField(
model_name="vote",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
),
]

View file

@ -81,6 +81,7 @@ class Option(models.Model):
voters = models.ManyToManyField( voters = models.ManyToManyField(
settings.AUTH_USER_MODEL, settings.AUTH_USER_MODEL,
related_name="votes", related_name="votes",
through="Vote",
) )
# For now, we store the amount of votes received after the election is tallied # For now, we store the amount of votes received after the election is tallied
nb_votes = models.PositiveSmallIntegerField(_("nombre de votes reçus"), default=0) nb_votes = models.PositiveSmallIntegerField(_("nombre de votes reçus"), default=0)
@ -89,6 +90,11 @@ class Option(models.Model):
ordering = ["id"] ordering = ["id"]
class Vote(models.Model):
option = models.ForeignKey(Option, on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
# ############################################################################# # #############################################################################
# Modification of the base User Model # Modification of the base User Model
# ############################################################################# # #############################################################################