Answer the question
In order to leave comments, you need to log in
Get all articles and comment from user?
I have two models
class Article(models.Model):
connect = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField('title', max_length=50)
text = models.CharField('text', max_length=360)
pud_date = models.DateTimeField('pud_date')
def __str__(self):
return self.title
class CommentArticle(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
think = models.ForeignKey(Article, on_delete=models.CASCADE)
title = models.CharField('comment_title', max_length=50)
text = models.CharField('comment_text', max_length=360)
pud_date = models.DateTimeField('comment_pud_date')
def __str__(self):
return self.title
{
1:{
id:1,
user:'user1',
'title':'TITLE 1',
'text':"TEXT 1",
'pud_date':"2020.01.11",
'comment_article':{
id:2,
user:'user1',
"comment_title":'Com Title 2',
"comment_text":'Com Title 2',
"comment_pud_date""2020.05.11",
}
},
2:{
id:2,
user:'user2',
'title':'TITLE 2',
'text':"TEXT 2",
'pud_date':"2020.01.11",
'comment_article':{}
}
3:{
id:3,
user:'user2',
'title':'TITLE 3',
'text':"TEXT 3",
'pud_date':"2020.01.11",
'comment_article':{
id:1,
user:'user1',
"comment_title":'Com Title 1',
"comment_text":'Com Title 1',
"comment_pud_date""2020.03.11",
}
}
4:{
id:4,
user:'user1',
'title':'TITLE 4',
'text':"TEXT 4",
'pud_date':"2020.04.11",
'comment_article':{}
}
}
}
{
1:{
id:1,
user:'user1',
'title':'TITLE 1',
'text':"TEXT 1",
'pud_date':"2020.01.11", # Дата стать
'comment_article':{
id:2,
user:'user1',
"comment_title":'Com Title 2',
"comment_text":'Com Title 2',
"comment_pud_date""2020.05.11", # Новая дата комментария
}
},
4:{
id:4,
user:'user1',
'title':'TITLE 4',
'text':"TEXT 4",
'pud_date':"2020.04.11", # Дата статьи
'comment_article':{}
}
}
3:{
id:3,
user:'user2', # Другой пользователь но
'title':'TITLE 3',
'text':"TEXT 3",
'pud_date':"2020.01.11",
'comment_article':{
id:1,
user:'user1', # Комментарий нужного пользователя
"comment_title":'Com Title 1',
"comment_text":'Com Title 1',
"comment_pud_date""2020.03.11",
}
}
}
Answer the question
In order to leave comments, you need to log in
I think something like this:
...
think = models.ForeignKey(Article, on_delete=models.CASCADE, related_query_name ='comment_article')
...
user_id = 777
Article.objects.filter(Q(connect_id=user_id) || Q(comment_article__user_id=user_id))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question