Jacob Valenta
Jacob Valenta

Reputation: 6769

Django Cross Table Querysets

My model is defined as:

class Inventory(models.Model):
    items = models.ManyToManyField(Item, blank=True)
    scanned_items = models.ManyToManyField(Item, related_name='scanned_inventory_set', blank=True)

In a view, I am trying to get a list of all items that have not been scanned. Basically, it would have to be (items and not scanned_items). However, I do not know how to do the look up with the relationship between the two.

Upvotes: 1

Views: 579

Answers (1)

karthikr
karthikr

Reputation: 99620

You could do:

items_to_exclude = Inventory.objects.values_list('items', flat=True)
items = Items.objects.exclude(id__in=items_to_exclude)

Upvotes: 2

Related Questions