【发布时间】:2021-02-15 19:08:29
【问题描述】:
我正在尝试使用 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.
【问题讨论】: