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.
If I understand your question correctly, you want a distinct set of all items from all
ItemSetswhich contain a specificItem, excluding theItemitself from the returned set. Does that sound about right?Edit: Tested.
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: