JojoL
JojoL

Reputation: 141

Use a list as an object for a model, Django

I would like to know of it's possible to use a list as an object for a Model.

I'm developing a django app and I would like the users to be able to "like" other users, and keep a list of the users they like so I can render it in an other page.

So is there something like a "listField"?

Thank you for your help.

Upvotes: 0

Views: 66

Answers (2)

Max Jaderberg
Max Jaderberg

Reputation: 562

Definitely a many-to-many relationship would be best, and it really may be useful to add a through model to record the created times for the likes and be able to show them independently of users (like an activity stream) e.g. (not tested)

    class MyUser(User):
        ...
        likers = models.ManyToManyField('self', through='Like', 
                                       symmetrical=False, 
                                       related_name='liked_users')
        ...

    class Like(models.Model):
        liker = models.ForeignKey(MyUser, related_name='likes_given')
        liked_user = models.ForeignKey(MyUser, related_name='likes_received')
        ...

You might find this userful http://charlesleifer.com/blog/self-referencing-many-many-through/

Upvotes: 2

Marcin
Marcin

Reputation: 49846

No, what you want is a many-to-one relationship: https://docs.djangoproject.com/en/dev/topics/db/models/#many-to-one-relationships

Upvotes: 0

Related Questions