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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:06:07+00:00 2026-05-23T15:06:07+00:00

What I am trying to do is to write a query set that filters

  • 0

What I am trying to do is to write a query set that filters out unique parent/name combinations. I’m only interested in the first occurance of a unique parent name.

ID   PARENT TYPE       LIBTYPE NAME
1     1     project     1     book_a
4     4     project     2     book_b
5     4     project     2     book_c
6     4     project     2     book_d
7     4     project     4     book_c
8     5     project     2     book_e
9     5     project     4     book_e
10     7     project     0     book_f
11     7     project     0     book_g
12     7     project     6     book_h
13     6     user        1     book_i
14     6     project     1     book_j
15     6     project     1     book_k
16     7     project     5     book_h
17     7     project     8     book_h
18     7     project     7     book_h
19     7     project     9     book_h
20     7     project     1     book_h
21     8     project     1     book_a

So we started with a basic queryset..

vars = Variants.objects.filter(type="project")

This removed out the lone user.. Now in pure python I would just do this to filter this sucker.

vars = Variants.objects.filter(type="project")
new_vars = []
for idx, var in vars.enumerate():
    if var.name not in new_vars:
        new_vars.append((var.parent,var.name))
    else:
        del vars[idx]

In the end I should end up with the following ID’s (1,4,5,6,8,10,11,13,14,15,21) Obviously I can’t enumerate on a Queryset plus I’m sure there is a way to filter this in Django.

Can someone please shed some light on how to efficiently do this in Django?

  • 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-23T15:06:08+00:00Added an answer on May 23, 2026 at 3:06 pm

    Your so-called “pure Python” solution makes no sense, I’m afraid. For a start, enumerate is a built-in, so you would call enumerate(vars); secondly, you append a tuple to new_vars so var in new_vars will never be True; and thirdly, I can’t see what you’re trying to do with that del statement – you should never modify something you’re iterating through.

    A better solution in Python might be something like this:

    var_dict = {}
    for var in vars:
        if var.name not in var_dict:
            var_dict[var.name] = var
    
    return var_dict.values()
    

    which works just as well with a queryset.

    If I understand correctly, though, you’re after a db-level solution. That’s not possible, as aggregation functions work on the whole row being selected. You can get the unique values for (parent, name) but you can’t get the ID as well – because (id, parent, name) is not a unique combination.

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

Sidebar

Related Questions

I am trying to write a query that connects 3 tables. The first table
Im trying to write a query that updates a date only if the group
I am trying to write a query in Postgresql that pulls a set of
I'm trying to write a query that will set a bit value, based on
I'm trying to write a query that will pull back the two most recent
I'm trying to write a query that extracts and transforms data from a table
I'm trying to write a query that updates rows in a table if a
I am trying to write a SQL query that pulls from 3 tables and
I am trying to write a stored procedure that takes a table name as
I'm trying to write a query along these lines: UPDATE Table i2 SET value

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.