curtisp
curtisp

Reputation: 2325

Django ManyToManyField get records from related model where not in join table

I am using Django ManyToManyField to assign tags to photos. I put M2M field in Tags model:

class Tags(models.Model):
    tag = models.CharField(max_length=100, blank=True, null=True)
    photos = models.ManyToManyField(Photos)

It works fine. I can get all tags assigned to photo using:

photo = Photos.objects.get(id=int(photo_id))
photo_tags = photo.tags_set.all()

Then to give user a list of all tags so they can assign them photo I am using:

tag_list = Tags.objects.order_by('tag')

However, I'd like to limit this list to tags that are not yet assigned to the photo eg where tag is not in join table for that photo.

But stuck on what ORM logic to use here. Is there something like tags_set_none or filter(tags_set=None) ?

Upvotes: 0

Views: 273

Answers (1)

f43d65
f43d65

Reputation: 304

tags = Tags.objects.exclude(photos=photo)

Upvotes: 1

Related Questions