Проблема с обращением к таблице auth_user в Django с помощью необработанного запроса
I need to make an inner join on my tables and I need the users last name and first name from the auth_user
table whose are added to a project. Projects are collected in another table (Project) that is connected to a Profile
model with which I extended my User
model.
Еще я использую SqLite3, поэтому я написал запрос, который хорошо работает в SqLite Viewer:
SELECT last_name, first_name, projekt_id
FROM 'auth_user'
INNER JOIN stressz_profile ON
stressz_profile.user_id = auth_user.id
WHERE projekt_id=1
Я принял запрос к Django следующим образом:
resztvevok = Profile.objects.raw('SELECT username, projekt_id FROM auth_user AS resztvevok INNER JOIN stressz_profile ON stressz_profile.user_id = auth_user.id WHERE projekt_id=1')
Но когда я обновляю браузер, он говорит:
no such column: auth_user.id
models.py
class Profile(models.Model):
def __str__(self):
return str(self.user)
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
projekt = models.ForeignKey(Projekt, on_delete=models.CASCADE, default=3)
...
Вы получаете данные из таблицы auth_user, но ссылаетесь на экземпляр модели класса Profile. Я думаю, что это должно быть User.objects.raw(...)
Думаю, проблема в том, что вы не используете псевдоним resztvevok
при обращении к колонке id
:
resztvevok = Profile.objects.raw('SELECT username, projekt_id FROM auth_user AS resztvevok INNER JOIN stressz_profile ON resztvevok.id = stressz_profile.user_id WHERE projekt_id=1')