Suppose that I have a very basic user model with name and age:
class User(models.Model):
name = CharField() # e.g. John Smith
age = IntegerField() # e.g. 21
I want to filter users 18+ y/o and for each of them add a special attribute namesakes
(for instance, for "John Smith"
it would be something like ["John Williams", "John for Neumann", ...]
).
I know, how to do this in N+1 requests:
for user in User.objects.filter(age__gt=18):
user.namesakes = User.objects.filter(name__startswith=user.name.split()).\
all()
But how do I do this in one-ish request? Ideally, for each User
object in queryset I'd like to create a custom attribute namesakes
that contained a queryset of namesakes. This is very similar to what annotate() or prefetch_related() with to_attr do, but not exactly.
I'd also prefer to avoid using raw SQL, if possible.
Thank you.