S
S
sortfact3332020-11-09 13:27:41
Django
sortfact333, 2020-11-09 13:27:41

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

And I want to get all posts from a certain user comments to posts as well as those posts to which this user left a comment.
In other words, if I have
{
  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':{}
    }
  }
}


Then I get for user1
{
  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

1 answer(s)
A
Antonio Solo, 2020-11-09
@solotony

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 question

Ask a Question

731 491 924 answers to any question