mardi 12 avril 2016

Modelling results of an election in Django

I'm trying to figure out how to model the an election in Django. I have a model Candidate. There have to be exactly two candidates, so the definition is as follows:

class Candidate(models.Model):
    name = models.CharField(max_length=50)
    surname = models.CharField(max_length=50)
    def __str__(self):
        return self.name + ' ' + self.surname
    def clean(self):
        if (len(Candidate.objects.all()) > 1):
            raise ValidationError(
                "There are two candidates already"
            )

And I want to have a model CountyResult, with the following fields: county (foreign), number of residents, number of residents eligible for voting etc. and with the final results of the election in this county.

I would like to be able to insert the election results in the admin site.

And my question is: how can I implement the Candidate -> votes mapping inside the CountyResult model? I was thinking about simply storing values first_candidate_votes, second_candidate_votes, but that's obviously not a great solution, since I would like to be able to remove a candidate.

I was also thinking about adding a SingleResult model with a foreign key candidate and somehow 'put it' inside the CountyResult class, but there is no such thing as a two-to-one relation in Django

I would appreciate any hints. I'm new to Django, so it's almost certain that the solution is rather easy and technical




Aucun commentaire:

Enregistrer un commentaire