Hi Mr Mike.
Can you please share you knowledge here to help me?
On Sat, Jun 26, 2021 at 6:54 AM Mike Dewhirst <miked@dewhirst.com.au> wrote:
--HE lmbo pop l9m6oo9k--(Unsigned mail from my phone)-------- Original message --------From: DJANGO DEVELOPER <abubakarbryar@gmail.com>Date: 26/6/21 04:38 (GMT+10:00)To: Django users <django-users@googlegroups.com>Subject: Re: DRF | Django | Up votes | Down VotesRyan Thank you so much. I will give it try. and let you know about the results--On Fri, Jun 25, 2021 at 8:59 PM Ryan Nowakowski <tubaman@fattuba.com> wrote:On Thu, Jun 24, 2021 at 07:15:09AM -0700, DJANGO DEVELOPER wrote:
> Hi Django experts.
> I am building a Django, DRF based mobile app and I want to have
> functionality of up votes and down votes for posts that user will post.
> So what I want to say here that, if an user upvotes a post then it should
> be get higher ranks as quora questions do. and if there are more down votes
> than up votes then a post should be moved down ward rather than going up.
> I have applied a logic already here and shared the screenshots as well. but
> I am sure that there is a better logic or solution for this problem.
Do you want your users to be able to change or delete their vote?
In that case, you'll need to keep track of who voted what. Maybe create
a model like this:
class VoteQuerySet(models.QuerySet):
def down(self):
"""Return only the down votes"""
return self.filter(value__lt=0)
def up(self):
"""Return only the up votes"""
return self.filter(value__gt=0)
def down_total(self):
"""Return the down vote total as a negative number"""
return self.down().aggregate(Sum('value'))['value__sum']
def up_total(self):
"""Return the up vote total as a positive number"""
return self.up().aggregate(Sum('value'))['value__sum']
def total(self):
"""Return the total of all up and down votes"""
return self.aggregate(Sum('value'))['value__sum']
class Vote(models.Model):
"""A user's up or down vote for a post
"value" should be -1 for a down vote and 1 for an up vote
"""
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
value = models.SmallIntegerField()
objects = VoteQuerySet.as_manager()
class Meta:
unique_together = (user, post)
def vote_up(self):
self.value = 1
def vote_down(self):
self.value = -1
You can then vote like this:
vote = Vote(user=user, post=post)
vote.vote_up() # or vote.vote_down()
vote.save()
Here's a post's up vote count:
post.vote_set.up_total()
You can change a user's vote like this:
vote = user.vote_set.get(post=post)
vote.vote_down()
... or delete:
vote.delete()
Any finally, here's how you can sort your posts:
Post.objects.all().annotate(vote_total=Sum('vote__value')).order_by('-vote_total')
Caveat: code untested
Hope this helps!
Ryan
--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20210625155946.GC16290%40fattuba.com.
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKPY9pnmqN46L93O38djOKdCKP3Vp%2B_vHLetC479zDxuOU6x_g%40mail.gmail.com.
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/60d688c8.1c69fb81.bbb11.3564SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKPY9pmw1uYC7FC7zGj7YBVAYF__ZDZOagoyau9ajOtBr%2BJAeg%40mail.gmail.com.
No comments:
Post a Comment