0

Hey guys I'm creating a primitive social media app and I'm trying to create an array in my table that has a list of usernames that the user will be following so that their feed shows only posts from users that they are following.

How can I do this? Right now I only have integers and strings but what if I need an array or a data type that can store multiple values?

Here is my code. I want "follows" to be the array (or multiple value) field.

class User(AbstractUser):
    pass
    follows = models.CharField(max_length=64, default=0)
    followers = models.IntegerField(default=0)
1
  • does anyone have a better solution? I'm not sure how to create all these tables and have them work together. Sounds too complex. I'd rather just have a list or an array full of values. How do I do that? Commented Nov 23, 2020 at 19:32

1 Answer 1

1

You can use a many-to-many relationship or an intermediary model where you can add created information:

models.py:

class Follow(models.Model):
    follower = models.ForeignKey(User, related_name = 'follow_from_set', on_delete = models.CASCADE)
    following = models.ForeignKey(User, related_name = 'follow_to_set', on_delete = models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your help but I'm not sure how this works. How does this add many different fields? I would rather just have an array than multiple tables. I don't get how I would utilize the table this way. can you tell me more?
for instance, I need to be able to print out all the posts from users that a user is following. I don't think this table helps me do that.
Indeed, my answer is really short. I'll send the code in the next days.
In a hurry ? stackoverflow.com/questions/64995388/… ? so you can check the code from Antonio Melé here : github.com/PacktPublishing/Django-3-by-Example/tree/master/… That's a basic social media with your features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.