0

In this Image I show My actual problem occurs in usr_post means how I can simplify the database table for user-generated content or how can I design the table for user_generated content, Note I want to store every individual user-generated content :

**In this Image I show My actual problem occurs in usr_post means how I can simplify the database table for user-generated content or how can I design the table for user_generated content, Note I want to store every individual user-generated content **

Note that I am using Django framework and its backend support by PostgreSQL database means I am using PostgreSQL database

One to Many Relation will not work in my case here is a screenshot of unexpected output error_image

here is my model configuration

class User(models.Model):
first_name = models.CharField(max_length=40)
last_name = models.CharField(max_length=40)
usr_name = models.CharField(max_length=40)
status = models.IntegerField(default=00)
verify_type = models.CharField(max_length=10, default='E')
login_value = models.CharField(max_length=50, default='NA')
login_alternate = models.CharField(max_length=15)
country_code = models.CharField(max_length=15)
area_code = models.CharField(max_length=15)
password = models.CharField(max_length=200)
created_at = models.DateTimeField(auto_now=True)
usr_post = models.ForeignKey(UserPost, blank=True, null=True, on_delete=models.SET_NULL)
0

This is a classic 1:n-relationship between the users (1) and the content (n), for which Django has exellent support and documentation: Django docs on 1:n-relationships.

6
  • This will not work in a large amount of data. I already think about that but it does not work in my case. Mar 5 at 13:43
  • @Vijay This is the 'industry standard' solution and it absolutely works with practically any amount of data. For me it does with 1.000 to 1 relations and it is blazingly fast. Why shouldn't it work for you? What exactly is the problem? Mar 5 at 17:31
  • OK, I will try it. Mar 5 at 17:48
  • It's not working I updated the question, You can check it Mar 6 at 5:35
  • @Vijay It took a while but I think I only just now understood that you want to put the posts belonging to individual users into separate tables, right? This is not the way to do it. Take one joint table for your UserContent model and put all users' content into that table. Furthermore, can you also add the definition of the UserContent model to your question, and when posting output: please do not crop to a point where you cannot see what model's data is shown. Finally, when saying "unexpected output", could you please say what the expected output should be? Thx, Andreas Mar 6 at 21:47
0

I can't comment so I'll post here. If I understood you correctly, you wish to know which posts each user created? If so.. create another table "user_posts" which will have a foreign key with a user_id and then all your post data, Then if you want to find which posts are associated with each user you can use a JOIN.

2
  • I want an efficient way to do that Mar 5 at 13:33
  • What's more efficient than this? Its a one to many relationship. One user can have many posts. Then all you have to do is a JOIN query to receive all the posts that are associated with a user id.
    – Ofek
    Mar 5 at 14:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.