Форум сайта python.su
Все верно…. Спасибо… требуется создание алиаса.
Офлайн
tmtНа данный код идет ошибка - мне кажется в данном примере перепутано название поля и алиас… первым идет название колонки
Если не выводит, значит не совпадают названия полей в модели и в выборке.
Добавте в выборке алиасы для полей через “as” чтоб названия совпадали или через аргумент “translations” в raw() и будет вам щастье.
Matching is done by name. This means that you can use SQL's AS clauses to map fields in the query to model fields. So if you had some other table that had Person data in it, you could easily map it into Person instances:As long as the names match, the model instances will be created correctly.>>> Person.objects.raw('''SELECT first AS first_name,
... last AS last_name,
... bd AS birth_date,
... pk as id,
... FROM some_other_table)
Alternatively, you can map fields in the query to model fields using the translations argument to raw(). This is a dictionary mapping names of fields in the query to names of fields on the model.
Прочитайте внимательно http://docs.djangoproject.com/en/dev/topics/db/sql/#mapping-query-fields-to-model-fields еще раз.
Офлайн