4 ms·
how is it called in the docs ?
by agumonkey 3y ago
how is it called in the docs ?
- OJFord 3y agoIf I understand GP correctly, it's 'performing raw SQL queries', or specifically here, 'mapping query fields to model fields': https://docs.djangoproject.com/en/4.2/topics/db/sql/#mapping-query-fields-to-model-fields https://docs.djangoproject.com/en/4.2/topics/db/sql/#mapping... Basically you call `.raw("...")` from some model's queryset, but there's no requirement you actually query that model's table at all. class SomeModel: name = models.CharField() class OtherModel: foobar = models.CharField() SomeModel.objects.raw("select foobar as name from thisapp_othermodel") will yield `SomeModel`s with `name`s that are actual `OtherModel` `foobar` values.
- Izkata 3y agoYes, but also it's been a long time since I've had any reason to do this, and had gotten "managed = False" mixed up with abstract classes. Abstract classes won't let you do this, but you probably want "managed = False" to prevent migrations from doing stuff in the database, if it's going to be a reporting-only query that doesn't have a backing table. Also you need to return an "id" column since Django needs a primary key. On the flipside, you can put that query in the database as a VIEW and point the model at it, also with "managed = False".
- OJFord 3y agoOh yes I forgot about that, that can be annoying. But of course, when it's annoying because it doesn't matter, it doesn't matter and you can pass anything back `as id`.