Answer the question
In order to leave comments, you need to log in
Python Django, how to pull news by tags relative to many to many?
I try like this:
tags = News.objects.filter(tags__id=self.object.id)
tags = models.ManyToManyField(Tags)
Answer the question
In order to leave comments, you need to log in
Tag model:
class Tag(models.Model):
name = models.CharField(max_length=25, default=1)
class Meta:
verbose_name = 'Тег'
verbose_name_plural = 'Теги'
def __str__(self):
return self.name
class Article(models.Model):
...
article_tags = models.ManyToManyField(Tag, related_name='tags_for_article')
...
Вьюшка
articlitem = Article.objects.all()
Шаблон
{% for tag in articlitem.article_tags.all %}
<p class="articles_tags">{{ tag.name }}</p>
{% endfor %}
additional = Article.objects.filter(article_tags__in=articlitem.article_tags.all()).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question