tortoise-orm
tortoise-orm copied to clipboard
Can't find anyway to filter model with many to many field.
I tried so many times but can't get it working.. I was expecting tortoise to be same like django-orm but it is not acting like that.
My Models
class User(models.Model):
id = SnowflakeField(pk=True)
username = fields.CharField(max_length=255, unique=True)
firstname = fields.CharField(max_length=25)
lastname = fields.CharField(max_length=25)
email = EmailField(required=True, unique=True)
password = fields.CharField(max_length=4096)
class Channel(models.Model):
id = SnowflakeField(pk=True)
recipients = fields.ManyToManyField("api.User", related_name="recipients")
Now I am having two recipients A and B and I want to filter channel which contains both recipients A and B. I tried using nested filter functions but it doesn't seem to work... While in django-orm it works perfectly.
Is there any way to achieve this? Or I have to write custom queries for that?
From tortoise.expression import Q
filters = [Q(Q(recipients__id=user_1.id) & Q(recipients__id=user_2.id))]
await Channel.filter(*filters)
Would the above not work?