MU
Maciej UrmańskiНаурыз 10, 2019, 1:01 Т.Қ.
Django list of subscribed objects
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)
class Board(models.Model): title = models.CharField(max_length=255, verbose_name='Tytuł') slug = AutoSlugField(populate_from='title', unique=True) image = models.ImageField(upload_to='board-cover', verbose_name='Tło kategorii', null=True, blank=True) body = models.TextField(verbose_name='Opis kategorii') author = models.ForeignKey(User, on_delete=models.CASCADE) created_at = models.DateTimeField(auto_now_add=True) class Subscribe(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='subscribed_users') board = models.ForeignKey(Board, on_delete=models.CASCADE, related_name='subscribed_boards') created_at = models.DateTimeField(auto_now_add=True) subscribe = models.BooleanField(default=False) class Subject(models.Model): title = models.CharField(max_length=255, verbose_name='Tytuł') slug = AutoSlugField(populate_from='title', unique=True) body = models.TextField(blank=True, verbose_name='Treść') image = models.ImageField(upload_to='subject', null=True, blank=True) author = models.ForeignKey(User, on_delete=models.CASCADE) active = models.BooleanField(default=True) created_at = models.DateTimeField(auto_now_add=True) board = models.ForeignKey(Board, on_delete=models.CASCADE, related_name='subjects', verbose_name='Kategoria') votes = GenericRelation(LikeDislike, related_query_name='subjectsvotes') class Embed(models.Model): url = models.URLField(max_length=255) title = models.CharField(max_length=255) description = models.TextField() thumbnail_url = models.URLField(max_length=255) html = models.TextField() author = models.ForeignKey(User, on_delete=models.CASCADE) created_at = models.DateTimeField(auto_now_add=True) board = models.ForeignKey(Board, on_delete=models.CASCADE, blank=True, null=True, related_name='embeds') votes = GenericRelation(LikeDislike, related_query_name='embedvotes')
In my views I try something like this:
def feed(request): embeds = Embed.objects.filter(author=request.user) subscribed_ids = request.user.subscribed_users.values_list('id', flat=True) actions = embeds.filter(author_id__in=subscribed_ids) actions = actions[:10] return render(request, 'boards/feed.html', {'actions': actions})
but nothing show up.
Рекомендуем хостинг TIMEWEB
Стабильный хостинг, на котором располагается социальная сеть EVILEG. Для проектов на Django рекомендуем VDS хостинг.Ол саған ұнайды ма? Әлеуметтік желілерде бөлісіңіз!
Пікірлер
AD
- Akiv Doros
- Қар. 11, 2024, 2:58 Т.Қ.
C++ - Тест 004. Указатели, Массивы и Циклы
- Нәтиже:50ұпай,
- Бағалау ұпайлары-4
m
- molni99
- Қаз. 26, 2024, 1:37 Т.Ж.
C++ - Тест 004. Указатели, Массивы и Циклы
- Нәтиже:80ұпай,
- Бағалау ұпайлары4
m
- molni99
- Қаз. 26, 2024, 1:29 Т.Ж.
C++ - Тест 004. Указатели, Массивы и Циклы
- Нәтиже:20ұпай,
- Бағалау ұпайлары-10
Соңғы пікірлер
ИМ
Django - Оқулық 017. Теңшелген Django кіру беті Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
Игорь МаксимовҚар. 22, 2024, 11:51 Т.Ж.
Evgenii LegotckoiҚаз. 31, 2024, 2:37 Т.Қ.
Qt Creator көмегімен fb3 файл оқу құралы Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html
ИМ
Django - Сабақ 064. Python Markdown кеңейтімін қалай жазуға болады Приветствую Евгений! У меня вопрос. Можно ли вставлять свои классы в разметку редактора markdown? Допустим имея стандартную разметку: <ul> <li></li> <li></l…
Игорь МаксимовҚаз. 5, 2024, 7:51 Т.Ж.
QML - Сабақ 016. SQLite деректер қоры және онымен QML Qt-та жұмыс істеу Здравствуйте, возникает такая проблема (я новичок): ApplicationWindow неизвестный элемент. (М300) для TextField и Button аналогично. Могу предположить, что из-за более новой верси…
Енді форумда талқылаңыз
Mosquito Spray System Effective Mosquito Systems for Backyard | Eco-Friendly Misting Control Device & Repellent Spray - Moogo ; Upgrade your backyard with our mosquito-repellent device! Our misters conce…
Evgenii LegotckoiМаусым 24, 2024, 3:11 Т.Қ.
t
google domain [url=https://google.com/]domain[/url] domain [http://www.example.com link title]
tonypeachey1Қар. 15, 2024, 6:04 Т.Ж.
NSProjectМаусым 4, 2022, 3:49 Т.Ж.
IscanderCheҚаз. 31, 2024, 3:43 Т.Қ.
Hi,
it looks rightly.
Can you show print out of def feed. For example
May be you can write, I'm not sure
Hmm:
I print this and actions is empty. I try also with list and still the same.
may be, you want filter board instead of embed?
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
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).