Returning a subset of reverse FK #487
-
Is possible to return just a subset (for example first 10), of a reverse FK relation. I have some data with many reverse FK (to 10.000) when requesting the relationship it completely hang up my database. Is possible to set a limit, or is possible to in a following request load only a arbritrary number of reverse FK keys? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yes, reverse FK expose QuerysetProxy which behaves like QuerySet (except it's pre-filtered for given parent model). I.e.: class User(ormar.Model):
class Meta:
tablename = "users"
metadata = metadata
database = database
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
properties = ormar.JSON(nullable=True)
class Post(ormar.Model):
class Meta:
tablename = "posts"
metadata = metadata
database = database
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
created_by: Optional[User] = ormar.ForeignKey(User)
# you can load children as you wish
user = await User.objects.get()
await user.posts.limit(10).all() You can construct most of the same queries through relation like in normal query: https://collerek.github.io/ormar/relations/queryset-proxy/ |
Beta Was this translation helpful? Give feedback.
Yes, reverse FK expose QuerysetProxy which behaves like QuerySet (except it's pre-filtered for given parent model).
So you can construct all kinds of queries on relations too.
I.e.: