Django ORM Accessing related many-to-many objects through queryset

Django ORM Accessing related many-to-many objects through queryset

In my Django app, I have the following two models for events which can be given an arbitrary number of tags.

In my Django app, I have the following two models for events which can be given an arbitrary number of tags.

class Tag(models.Model):
  # ...

class Event(models.Model):

...

tags = models.ManyToManyField("Tag")

I also have a page where a user should be able to select a number of tags and then view all events associated with that tag. Given a list of tag IDs, can I use the Django ORM to get a list of (distinct) events associated with at least one of the given tags?

If not, I also tried achieving this with the following raw SQL

event.objects.raw("""
  SELECT *
  FROM   app_name_event_tags t
  WHERE  t.tag_id IN %s
""", selected_tag_ids)

There are two problems with this:

1) The events returned won't be distinct

2) My syntax for the placeholder for the list selected_tag_ids is incorrect

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Python Django Tutorial | Django Course

🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...

Python with SQL Server Database

In this article you will learn: How to connect through SQL Server Database using Python? How can we store some information into the SQL Server database using Python? How can we perform an update and delete operations? How we will retrieve stored information from the SQL Server database?