python - 不支持为 CharField 查找“xx”或不允许在该字段上加入
问题描述
我正在尝试使用 AJAX GET 请求获取用户创建的列表。但是,我的过滤返回了这个问题:
Unsupported lookup 'user' for CharField or join on the field not permitted.
我不确定这里出了什么问题。
这是我的模型:
class UserList(models.Model):
list_name = models.CharField(max_length=255)
user = models.ForeignKey(User, on_delete=models.CASCADE) #is this okay?
def __str__(self):
return self.list_name
class UserVenue(models.Model):
venue = models.ForeignKey(mapCafes, on_delete=models.PROTECT)
list = models.ForeignKey(UserList, on_delete=models.PROTECT)
class Meta:
unique_together = ['list','venue']
这是views.py:
# dashboard
def get_userlists(request):
template_name = '/testingland/dashboard/'
username = None
if request.user.is_authenticated:
username = request.user.username
print(username)
list = request.GET.get('userlist', None)
qs = UserList.objects.filter(list_name__user=username)
return qs
FWIW 这里是 ajax 调用:
const showUserLists = function(){
document.getElementById("userLists")
console.log('The Space Exists')
$.ajax({
type: 'GET',
url: '/electra/get_userlists/',
data: {
},
success: function (data) {
console.log(data);
}
});
};
追溯:
Traceback (most recent call last):
File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users//Desktop/Coding/anybody/anybody1/testingland/views.py", line 117, in get_userlists
qs = UserList.objects.filter(list_name__user=username)
File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/query.py", line 942, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/query.py", line 962, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, *args, **kwargs)
File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/query.py", line 969, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1358, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1380, in _add_q
split_subq=split_subq, check_filterable=check_filterable,
File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1319, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1159, in build_lookup
lhs = self.try_transform(lhs, lookup_name)
File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1200, in try_transform
"permitted%s" % (name, output_field.__name__, suggestion)
django.core.exceptions.FieldError: Unsupported lookup 'user' for CharField or join on the field not permitted.
解决方案
您正在过滤 a UserList
,因此list_name
a 的 aUserList
是 a CharField
,因此使用list_name__user
没有意义。你过滤:
qs = UserList.objects.filter(user=request.user)
获得UserList
给定的所有 s user
。
注意:通常使用
settings.AUTH_USER_MODEL
[Django-doc]引用用户模型比直接使用User
模型 [Django-doc]更好。有关更多信息,您可以查看文档的引用User
模型部分。
推荐阅读
- sql - Postgres:在带有“WITH数据”的查询中使用NULLIF
- python - 范围问题,可以访问对象属性但不能访问对象实例
- amazon-web-services - API 网关:在 TEST 按钮上确定,在 Curl 上显示 500
- docker - docker login 失败:tls: server selected unsupported protocol version 301
- python - 读取 CSV 并分配变量的程序
- amazon-web-services - 如何为 ECS 添加带有应用程序负载均衡器的 AWS API 网关?
- java - 重写 gradle 代码以兼容 5.1
- git - IntelliJ 中的交互式预提交挂钩
- visual-studio-code - VSCode,如何在以下声明后备份源代码树?
- php - PHP 使用 preg_replace_callback 短代码函数返回错误的元素顺序