Allow users to vote up, down or nothing

40 views
Skip to first unread message

Robin Lery

unread,
Jun 6, 2016, 11:27:45 AM6/6/16
to django...@googlegroups.com
Hello,
I have made an app to create article. This is the model:

class Article(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    title = models.CharField(max_length=150)
    body = models.TextField()
    pub_date = models.DateTimeField(default=timezone.now)
    tags = models.ManyToManyField(Tag)
    article_image = models.ImageField(upload_to=get_upload_file_name, blank=True)


Here I want to setup a voting system for each app. There I would the user to vote up, or vote down, or do nothing. Boolean field would have been great, but since an article can have 3 choices of votes  (vote up / vote down / no vote ), how should I go about doing this? Your help and guidance will be very much appreciated.

Thank you.

Virus-free. www.avast.com

Avraham Serour

unread,
Jun 6, 2016, 12:36:03 PM6/6/16
to django-users

Maybe a nullable boolean field

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B4-nGo-pNLjsEoE5bVT%3DaGxcJGcCm9nvx%2Bqjj8J6rT2J%3Dgq%3DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

ludovic coues

unread,
Jun 6, 2016, 4:27:05 PM6/6/16
to django...@googlegroups.com
I will assume user can vote a specific article. I would go for another model, like that

class VoteForArticle(models.Model):
    DOWN = 0
    NEUTRAL = 1
    UP = 2
    VOTES = ((DOWN, "down"), (NEUTRAL, "neutral)", (UP, "up"))
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    article = models.ForeignKey("Article")
    vote = models.IntegerField(choices=VOTES)

If you are concerned with space, use a models.CharField with max_length = 1


For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42
Reply all
Reply to author
Forward
0 new messages