MU
10 марта 2019 г. 23:01

Django list of subscribed objects

django, Django, python

Hi, I try to show user object from category which they subscribe. But don't know exacly how.

I have four models:
Board ( category )
Subject
Embed and Subscribe (foreign to user and to board)

  1. class Board(models.Model):
  2. title = models.CharField(max_length=255, verbose_name='Tytuł')
  3. slug = AutoSlugField(populate_from='title', unique=True)
  4. image = models.ImageField(upload_to='board-cover', verbose_name='Tło kategorii', null=True, blank=True)
  5. body = models.TextField(verbose_name='Opis kategorii')
  6. author = models.ForeignKey(User, on_delete=models.CASCADE)
  7. created_at = models.DateTimeField(auto_now_add=True)
  8.  
  9. class Subscribe(models.Model):
  10. user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='subscribed_users')
  11. board = models.ForeignKey(Board, on_delete=models.CASCADE, related_name='subscribed_boards')
  12. created_at = models.DateTimeField(auto_now_add=True)
  13. subscribe = models.BooleanField(default=False)
  14.  
  15. class Subject(models.Model):
  16. title = models.CharField(max_length=255, verbose_name='Tytuł')
  17. slug = AutoSlugField(populate_from='title', unique=True)
  18. body = models.TextField(blank=True, verbose_name='Treść')
  19. image = models.ImageField(upload_to='subject', null=True, blank=True)
  20. author = models.ForeignKey(User, on_delete=models.CASCADE)
  21. active = models.BooleanField(default=True)
  22. created_at = models.DateTimeField(auto_now_add=True)
  23. board = models.ForeignKey(Board, on_delete=models.CASCADE, related_name='subjects', verbose_name='Kategoria')
  24. votes = GenericRelation(LikeDislike, related_query_name='subjectsvotes')
  25.  
  26. class Embed(models.Model):
  27. url = models.URLField(max_length=255)
  28. title = models.CharField(max_length=255)
  29. description = models.TextField()
  30. thumbnail_url = models.URLField(max_length=255)
  31. html = models.TextField()
  32. author = models.ForeignKey(User, on_delete=models.CASCADE)
  33. created_at = models.DateTimeField(auto_now_add=True)
  34. board = models.ForeignKey(Board, on_delete=models.CASCADE, blank=True, null=True, related_name='embeds')
  35. votes = GenericRelation(LikeDislike, related_query_name='embedvotes')
  36.  

In my views I try something like this:

  1. def feed(request):
  2. embeds = Embed.objects.filter(author=request.user)
  3. subscribed_ids = request.user.subscribed_users.values_list('id', flat=True)
  4.  
  5. actions = embeds.filter(author_id__in=subscribed_ids)
  6. actions = actions[:10]
  7.  
  8. return render(request, 'boards/feed.html',
  9. {'actions': actions})

but nothing show up.

2

Вам это нравится? Поделитесь в социальных сетях!

5
Evgenii Legotckoi
  • 11 марта 2019 г. 14:43

Hi,

it looks rightly.

Can you show print out of def feed. For example

  1. def feed(request):
  2. embeds = Embed.objects.filter(author=request.user)
  3. print(embeds)
  4. subscribed_ids = request.user.subscribed_users.values_list('id', flat=True)
  5. print(subscribed_ids)
  6.  
  7. actions = embeds.filter(author_id__in=subscribed_ids)
  8. print(actions)
  9. actions = actions[:10]
  10.  
  11. return render(request, 'boards/feed.html',
  12. {'actions': actions})

May be you can write, I'm not sure

  1. def feed(request):
  2. embeds = Embed.objects.filter(author=request.user)
  3. print(embeds)
  4. subscribed_ids = request.user.subscribed_users.values_list('id', flat=True)
  5. print(subscribed_ids)
  6.  
  7. actions = embeds.filter(author__in=list(subscribed_ids))
  8. print(actions)
  9. actions = actions[:10]
  10.  
  11. return render(request, 'boards/feed.html',
  12. {'actions': actions})
    MU
    • 11 марта 2019 г. 15:46
    • (ред.)

    Hmm:

    1. <QuerySet [<Embed: Embed object (11)>, <Embed: Embed object (10)>, <Embed: Embed object (9)>, <Embed: Embed object (8)>, <Embed: Embed object (1)>]>
    2. <QuerySet [2, 3, 5]>
    3. <QuerySet []>

    I print this and actions is empty. I try also with list and still the same.

      Evgenii Legotckoi
      • 11 марта 2019 г. 15:54

      may be, you want filter board instead of embed?

      1. actions = Board.objects.filter(author__in=list(subscribed_ids))
        MU
        • 11 марта 2019 г. 16:06
        • (ред.)

        Now I get Board instead of embed. :D

        Hmm in evileg You have too subscribe option to category articles I see. How You do this? :)

        Before I try to add manytomanyfield to board instead of create new model Subscribe but i don't know how to query that way. Now I see in subscribe model too don't know how. :D

          Evgenii Legotckoi
          • 11 марта 2019 г. 16:17
          • (ред.)

          I have created subscriptions similar like LikeDislike model, because of I need generic Subscription for all types of content (It may be category of articles, forum, bookmarks, forum topics and so on).

            Комментарии

            Только авторизованные пользователи могут публиковать комментарии.
            Пожалуйста, авторизуйтесь или зарегистрируйтесь