不允许在字段上查找CharField或join的不支持的“xx”

2024-10-02 20:36:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试获取一个由用户使用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

这里是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.

Tags: djangonameinpymodelslibpackagesline
1条回答
网友
1楼 · 发布于 2024-10-02 20:36:58

您正在筛选UserList,因此UserListlist_nameCharField,因此使用list_name__user没有意义。您可以使用以下选项进行筛选:

qs = UserList.objects.filter(user=request.user)

获取给定user的所有UserList


Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

相关问题 更多 >