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 7599991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:48:04+00:00 2026-05-30T22:48:04+00:00

Hi I am trying to write a django webapp that sits on top of

  • 0

Hi I am trying to write a django webapp that sits on top of a legacy database and so I cannot control too much my model fields and achieve functionality using new models.

I have many Documents with user given comma-separated-tags . I want to group “related” documents based on shared tags.

# Model to express legacy table
class Document(models.Model):
    id = models.BigIntegerField(primary_key= True)
    metadata_id = models.CharField(max_length=384)
    tags_as_csv = models.TextField()

# Created new Model tag_text extracted from tags_as_csv
class Tagdb(models.Model):
    tagid = models.BigIntegerField(primary_key=True)
    referencing_document = models.ForeignKey(Document)
    tag_text = models.TextField(blank=True)

So a Document would contain:

Document : 
id = 1 , 
metadata_id = "a1ee3df3600c6f77a6e851781f7e70c6" , 
tags_as_csv = "raw-data , high temperature , important"

The Tagdb would have entries such as

id , referencing_document , tag_text
1  , 1 , "raw-data" 
2  , 1 , "high temperature"
3  , 1 , "important"
4  , 2 , "important"
5  , 2 , "processed-data"
6  , 3 , "important"
7  , 4 , "processed-data"

Now I want to extract all the Document objects that match tags corresponding to a parent document. Which I am doing using the following get_queryset method.

   def get_queryset(self, **kwargs):
        parent_document = Document.objects.get(id=self.kwargs['slug'])
        tags_in_parent_document = [x.tag_text for x in Tagdb.objects.filter(referencing_document=parent_document.id)]
        # This will contain all the Document ids that match all the tags
        queryset_with_duplicates = []
        for tag in tags_in_parent_document:
            queryset_with_duplicates.extend([x.referencing_document.id for x in Tagdb.objects.filter(tagtext__icontains=tag)])

        # Make sure we have only unique ids
        queryset_unique = set(queryset_with_duplicates)

        # Get all the Document objects 
        queryset = Document.objects.filter(id__in=queryset_unique)

        return queryset

My question is : is there a better way . Can I somehow get all the Documents that contain all the tags in the parent document and filter out the duplicates ( since multiple documents contain the same tag).

  • 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-05-30T22:48:06+00:00Added an answer on May 30, 2026 at 10:48 pm

    You’d better create two additional models: one for Tag and one for link between Tag and Document. If it’s somewhy unacceptable, you can use something like:

    Document.objects.filter(tagdb__tag_text__in=doc.tags_as_csv.split(' , ')).distinct()
    

    Plus, add model method for getting/setting tags, it will ease any possible refactoring.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a django inclusion tag that takes an arbitrary number of
I'm trying to write a Django-subclassed ModelChoiceField class that knows how to load itself,
I am trying to write a test that involves running a django-tasks task. The
I'm trying to design model in Django. I want that App can link to
I'm trying to write a Django URL pattern that matches 1 to n strings
I'm trying to write a custom command that works outside of Django projects. I
I’m trying to write a Django query that returns objects that match either of
I'm using Django to write a blog app, and I'm trying to implement a
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
Trying to write a couple of functions that will encrypt or decrypt a file

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.