Django过滤关系数据

时间:2019-05-14 11:00:08

标签: django django-models

我在Django中定义了一些具有关系的模型。当我想通过过滤关系数据来获取数据时,出现了错误。

型号:

class User(models.Model):
    UserId = models.CharField(max_length=255, primary_key=True)
    Username = models.CharField()
    RelatedPerson = models.ForeignKey(
        People,
        db_column='PersonId',
        on_delete=models.CASCADE,
    )

class Staff(models.Model):
    RelatedUser = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        db_column='StaffUserId',
        related_name='RelatedUser'
    )

我的查询是这样:

staff = Staff.objects.filter(RelatedUser__UserId=user_id).first()

我还尝试获取不是主键的其他字段,但该字段也不起作用:

staff = Staff.objects.filter(RelatedUser__UserId=user_id).first()

这是我得到的错误:

django.db.utils.ProgrammingError: ('42S22', "[42S22] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid column name 'id'. (207) (SQLExecDirectW); [42S22] [Microsoft][ODBC Driv
er 13 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)")

0 个答案:

没有答案