0

I am facing the following problem.

I have to entities,Proposal and User, a user can vote up or down several proposals and a proposal can have several votes from several users.

This relation between Proposal and User is Many to Many, the thing is that here I want to add an extra field to indicate if the Vote is positive or negative.

Is there are a way to do this in Django using ManyToManyField?, or the only way to do this is creating the model entity of Vote by hand like this:

    class Vote(models.Model):
    user = models.ForeignKey(User,related_name='voter',null=False)
    proposal = models.ForeignKey(Proposal,related_name='vote_to',null=False)
    opinion = models.BooleanField(blank=False,null=False)

And in case I have to do it by hand, how I can do for saying to Django that the primary key is the composition of the others Foreign keys

Yogi
  • 609
  • 1
  • 8
  • 21
  • Associating `User` directly with the `Proposal` does not very well indicates the precise logic of Vote. It is best to create a separate model `Vote`. – Paras Jain Apr 24 '18 at 19:00

1 Answers1

1

Creating a separate model Vote is a better way to do it, because a vote is specific to a particular proposal for a particular user, so it can't be directly linked to the Proposal model or to the User model.

See this article regarding mutiple column primary keys support in Django, and also this answer.

codeandfire
  • 445
  • 2
  • 9