简体   繁体   English

找不到PostgreSQL的Django身份验证用户

[英]Can't find django auth user with PostgreSQL

from django.contrib.auth.models import User as DjangoUser
class Ward(models.Model):
    user = models.ForeignKey(DjangoUser, related_name='wards')
    group = models.ForeignKey(Group, related_name='wards')

This is my django model and I use this filter. 这是我的django模型,我使用了此过滤器。

Group.objects.filter(wards__user=_user).all()

I used this code in sqlite3, it works well. 我在sqlite3中使用了此代码,效果很好。

But, it doesn't work in PostgreSQL. 但是,它在PostgreSQL中不起作用。

operator does not exist: character varying = integer LINE 1: ...rchive_ward"."group_id" ) WHERE "archive_ward"."user_id" = 1 运算符不存在:字符变化=整数LINE 1:... rchive_ward“。” group_id“)WHERE” archive_ward“。” user_id“ = 1

I think it is caused by user_id field in archive_ward tables. 我认为这是由archive_ward表中的user_id字段引起的。 I found this field's data type is character.varying(20). 我发现该字段的数据类型为character.varying(20)。

What can I do for this code? 我可以为该代码做什么?

Try removing the user table in the database and adding it again. 尝试删除数据库中的用户表,然后再次添加它。

create a new one from scratch. 从头开始创建一个新的。 Syncing database again will work.. 再次同步数据库即可。

or else You can do like this Way raw_query 否则,您可以通过这种方式raw_query

You cannot compare an integer with a varchar. 您不能将整数与varchar进行比较。 PostgreSQL is strict and does not do any magic typecasting for you. PostgreSQL是严格的,不会为您做任何魔术转换。 I'm guessing SQLServer does typecasting automagically (which is a bad thing). 我猜想SQLServer会自动进行类型转换(这是一件坏事)。

If you want to compare these two different beasts, you will have to cast one to the other using the casting syntax :: 如果要比较这两种不同的野兽,则必须使用强制转换语法将一种强制转换为另一种

The Postgres error means you're comparing an integer to a string: Postgres错误表示您正在将整数与字符串进行比较:

operator does not exist: character varying = integer

You could change the database model so user_id is of an integer type. 您可以更改数据库模型,以便user_id为整数类型。 Or you could cast the integer to string in Python: 或者,您可以在Python中将整数转换为字符串:

Group.objects.filter(wards__user=str(_user)).all()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM