User to Model relationship- implement a "like" feature on a Model

44 views
Skip to first unread message

amarshall

unread,
Aug 25, 2014, 10:40:41 PM8/25/14
to django...@googlegroups.com
Hi,

   I'm trying to implement likes on a Model in which 1 user will be able to like an a model (named "event") "once" and only once.

so, I have a model like this:

class Event(models.Model):

    user
= models.ForeignKey(User)
   
.         #other fields
   
.
    likes
= models.PositiveIntegerField(default=0,null=True)




How do I implement it so that only one user can "like" an event (increase by 1) ? Brainstorming I'm thinking should I make a new class called "like" and have it have "Event" as a foreign key and a oneToOne relationship with User ? not really sure how to go about doing this.

class Like(models.Model):
       
       
event = models.ForeignKey(Event)
       
       user
= model.ManyToMany(User)
       user_liked
= models.BooleanField(default=false)
       total_count
= models.PositiveIntegerField(default=0)


?   Then when an event is created a "Like" is created as well ? This may be doing to much and there's a simpler way?


      

Mario Gudelj

unread,
Aug 25, 2014, 10:57:09 PM8/25/14
to django...@googlegroups.com

The other way would be to simply wire a cookie to user's browser and use js to disable the the like button if the cookie is present if you can get away with it. Not sure if you need to store that info against the user to see which items they liked...

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/05ade3fc-f182-49be-a7d3-043fad68131b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

amarshall

unread,
Aug 26, 2014, 12:00:13 AM8/26/14
to django...@googlegroups.com
Hmm, That may work. I should have also noted one thing. I'm actually using Django as the backend for mobile application. Both Android and iOS. So I'd like to do something like this, in the simplest matter:

pseudocode:
 
      get information from the server.
      if this_user HAS NOT "liked"  this event ,
              event_likes_count += 1
     else
          keep the like count the same. make like button inactive

Tim Chase

unread,
Aug 26, 2014, 12:26:35 AM8/26/14
to django...@googlegroups.com, adria...@gmail.com
On 2014-08-25 17:00, amarshall wrote:
> Hmm, That may work. I should have also noted one thing. I'm
> actually using Django as the backend for mobile application. Both
> *Android* and iOS. So I'd like to do something like this, in the
> simplest matter:
>
> pseudocode:
>
> get information from the server.
> if this_user HAS NOT "liked" this event ,
> event_likes_count += 1
> else
> keep the like count the same. make like button inactive

You may want to try phrasing the problem as "select count of all the
likes where the event=$THIS_EVENT and the user != $THIS_USER" which
is pretty close to the underlying SQL and shouldn't be too hard to
translate into a a Django query. Something like this untested

# [models.py]
class Event(Model):
likes = ManyToMany(through="Like")
# ...

class Like(Model):
event = ForeignKey(Event)
user = ForeignKey(User)
class Meta:
unique_together = [
("event", "user"),
]

# [views.py]
this_event = #...
this_user = #...
Like.objects.filter(event=this_event).exclude(user=this_user).Count(id)

As mentioned, it's untested, but should get you pretty close.


-tkc




amarshall

unread,
Aug 26, 2014, 12:41:28 AM8/26/14
to django...@googlegroups.com, adria...@gmail.com, django...@tim.thechases.com
Thanks ! I'll get to work on that and see how it goes!

shmengie

unread,
Aug 26, 2014, 2:31:57 PM8/26/14
to django...@googlegroups.com
models.py:

class
Like(models.Model):
   
event = models.ForeignKey(Event)
    user
= model.ForeignKey(User)

    class Meta:
        unique_together = ("event", "user")

Now you can count Likes for a given event

likes = Like.objects.filter(event='BizBangBoom').annotate(number_of_entries=Count('entry'))

The unique_together Meta enforces limit of one like per user, per event.

If a user un-likes an event, delete the envent - user record

Reply all
Reply to author
Forward
0 new messages