Код: Выделить всё
from tortoise import Tortoise, fields, run_async
from tortoise.models import Model
class BaseModel(Model):
class Meta:
abstract = True
id = fields.IntField(pk=True, index=True)
class BaseProp(BaseModel):
class Meta:
abstract = True
name: str = fields.CharField(50, null=False, unique=True)
class Property(BaseProp):
class Meta:
table = "properties"
customs: fields.ManyToManyRelation["Custom"]
class Custom(BaseModel):
class Meta:
table = "customs"
short = fields.CharField(50, null=False, unique=True)
properties: fields.ManyToManyRelation["Property"] = fields.ManyToManyField(
"models.Property", related_name="customs"
)
async def make() -> None:
custom_one = await Custom.create(short="one")
custom_two = await Custom.create(short="two")
fine = await Property.create(name="fine")
light = await Property.create(name="light")
pink = await Property.create(name="pink")
await custom_one.properties.add(fine, light, pink)
await fine.customs.add(custom_two)
async def main() -> None:
await Tortoise.init(config=TORTOISE_CONFIG)
await Tortoise.generate_schemas()
await make()
result = await Custom.filter(properties__id__in=[1, 2, 3]).distinct()
print(result)
if __name__ == '__main__':
run_async(main())
Код: Выделить всё
> [, ]
Как получить только те Custom, которые есть ли все перечисленные идентификаторы свойств сразу, а не просто упомянуты хотя бы один раз?
Я также пробовал выражение Q:
Код: Выделить всё
result = await Custom.filter(
Q(
Q(properties__id=1),
Q(properties__id=2),
Q(properties__id=3)
)
).distinct()
print(result)
Подробнее здесь: https://stackoverflow.com/questions/783 ... -identifie