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

  • Home
  • SEARCH
  • 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 1027421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:14:19+00:00 2026-05-16T12:14:19+00:00

My models: class ItemSet(models.Model): name = models.CharField(max_length=30) item = models.ManyToManyField(Item) order = models.IntegerField(default=0) class

  • 0

My models:

class ItemSet(models.Model):
    name = models.CharField(max_length=30)
    item = models.ManyToManyField(Item)
    order = models.IntegerField(default=0)

class Item(models.Model):
    name = models.CharField(max_length=30)
    desc = models.CharField(max_length=100)

A set includes many items and a item can be in many sets.
So, how to get a list of items when we know the id of a item in some sets but itself?
Please give me some codes. Thank you very much!

Example:
We have two sets like this:
(1,2,3,4) and (2,3,5,7,9),
id = 3 then result = (1,2,4,5,7,9). Note: result does not include 3.

  • 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-16T12:14:19+00:00Added an answer on May 16, 2026 at 12:14 pm

    If I understand your question correctly, you want a distinct set of all items from all ItemSets which contain a specific Item, excluding the Item itself from the returned set. Does that sound about right?

    Edit: Tested.

    class ItemSet(models.Model):
        name = models.CharField(max_length=30)
        items = models.ManyToManyField('Item', related_name='item_sets')
        order = models.IntegerField(default=0)
    
    class Item(models.Model):
        name = models.CharField(max_length=30)
        desc = models.CharField(max_length=100)
    
    # First, get all sets containing the item we're interested in:
    item_sets = ItemSet.objects.filter(items__pk=item.pk)
    # Next, get all items belonging to those sets, excluding the item we're interested in:
    items = Item.objects.filter(item_sets__pk__in=item_sets).exclude(pk=item.pk).distinct()
    

    Note: This actually executes a single query (using Django 1.2.1), though, this might depend on your database backend. You can examine the generated SQL like so:

    >>> from django.db import connection
    >>> items._as_sql(connection)
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 532k
  • Answers 532k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For the top style to have any effect you need… May 17, 2026 at 12:07 am
  • Editorial Team
    Editorial Team added an answer If you are returning HTML and you expect to have… May 17, 2026 at 12:07 am
  • Editorial Team
    Editorial Team added an answer Try this.. Custom classes implemented by leonho. May 17, 2026 at 12:07 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In models: class Getdata(models.Model): title = models.CharField(max_length=255) state = models.CharField(max_length=2, choices=STATE, default=0) name =
Here's my code: class Publisher(models.Model): name = models.CharField( max_length = 200, unique = True,
My Models: class PromoNotification(models.Model): title = models.CharField(_('Title'), max_length=200) content = models.TextField(_('Content')) users = models.ManyToManyField(User,
My models: class Contact(models.Model): first_name = models.CharField(_(First name), max_length=30, ) last_name = models.CharField(_(Last name),
Consider the following models: class Person(models.Model): name = models.CharField(max_length=128) class Group(models.Model): name = models.CharField(max_length=128)
Example models: class Parent(models.Model): name = models.CharField() def __unicode__(self): return self.name class Child(models.Model): parent
My models: class Order(models.Model): ordered_by = models.ForeignKey(User) reasons = models.ManyToManyField(Reason) class Reason(models.Model): description =
my models class Auction belongs_to :item belongs_to :user, :foreign_key => :current_winner_id has_many :auction_bids end
I have the following models: class Post(models.Model): message = models.TextField() (etc.) class UserProfile(models.Model): user
Situation # Models class User < ActiveRecord::Base has_many :items end class Items < ActiveRecord::Base

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.