Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:47:51+00:00 2026-06-18T08:47:51+00:00

I’m working on search for my blog. Anyway, I’m having some problems with implementing

  • 0

I’m working on search for my blog. Anyway, I’m having some problems with implementing it.

I’m having two models:

class Article(models.Model):
    title = models.CharField(max_length=255)
    content = models.TextField()

class Tag(models.Model):
    article = models.ForeignKey(Article)
    content = models.CharField(max_length=255)

Actual search has two fields: phrase and tags. Phrase should look in Article.title and Article.content, but tag should look for articles that have Tag object by Tag.content.

I’m having a test too!

def test_by_phrase_and_tags(self):
    article_content = "spam"
    tag_content1 = "eggs"
    tag_content2 = "cheese"
    article1 = test_utilities.create_article(content=article_content)
    article2 = test_utilities.create_article(content=article_content)
    test_utilities.create_tag(article1, content=tag_content1)
    test_utilities.create_tag(article2, content=tag_content1)
    test_utilities.create_tag(article2, content=tag_content2)
    response = self.client.get(reverse("blog_search"), {
        "phrase": article_content,
        "tags": "{}, {}".format(tag_content1, tag_content2)
    })
    found_articles = response.context[-1]["found_articles"]
    self.assertEqual(found_articles, [article2])

It creates two articles with equal content, equal tag that’s set for both articles and unique tag that’s set only for 2nd article.

Then I request articles that have the content (both articles should match) and both tags (only 2nd article should match). Overall, I’m asserting that only 2nd article is returned.

I have tried a lot of things with raw SQL and Django ORM, but none seems to work.

With a sub-query:

SELECT * FROM blog_article
WHERE blog_article.content = "spam"
AND blog_article.id IN (
    SELECT blog_tag.article_id FROM blog_tag
    WHERE blog_tag.content = "eggs"
    OR blog_tag.content = "cheese"
);

With a join:

SELECT * FROM blog_article
JOIN blog_tag
ON blog_article.id = blog_tag.article_id
WHERE blog_article.content = "spam"
AND blog_tag.content = "eggs"
AND blog_tag.content = "cheese";

The same and other things with ORM…

So how can I get articles that have spam in title or content and have tags? I’m stumped.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:47:52+00:00Added an answer on June 18, 2026 at 8:47 am

    If i understand correctly you want to filter the articles by article.title or article.content with the phrase or with tags:

    First define a related_name for article in tags model:

    class Tag(models.Model):
        article = models.ForeignKey(Article, related_name='tags')
    

    Now query:

    from django.db.models import Q
    
    # supposed input
    phrase = 'my search string'
    tags = ['tag1', 'tag2']
    
    articles = Article.objects.filter(
       Q(title__icontains=phrase) |
       Q(content__icontains=phrase)) \
       .filter(tags__content__in=tags) \
       .annotate(num_tags=Count('tags')) \
       .filter(num_tags=len(tags))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have just tried to save a simple *.rtf file with some websites and
I am using JSon response to parse title,date content and thumbnail images and place
For some reason, after submitting a string like this Jack’s Spindle from a text
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.