There are following models:
class Schema(models.Model):
keys = models.ManyToManyField(Key, through='SchemaHasKey')
# ...
class Key(models.Model):
name = models.CharField(max_length=50)
# ...
I need select 10 first schemas with the keys, but my way is bad:
schemas = []
for schema in Schema.objects.all().order_by('pk')[:10]:
schema.key = schema.keys.all()
schemas.append(schema)
.select_related() is not work well:
schemas = Schema.objects.select_related().order_by('pk')[:10]
This can be done in a certain number of sql queries?
Here’s how I might do it until
prefetch_relatedfunctionality is officially launched.2 queries.