【问题标题】:Unsupported lookup 'xx' for CharField or join on the field not permittedCharField 的不支持查找“xx”或不允许在字段上加入
【发布时间】: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.

【问题讨论】:

    标签: python django


    【解决方案1】:

    您正在过滤UserList,因此UserListlist_nameCharField,因此使用list_name__user 没有意义。你过滤:

    qs = UserList.objects.filter(<b>user=request.user</b>)

    获取给定user的所有UserLists。


    注意:通常使用settings.AUTH_USER_MODEL [Django-doc] 引用用户模型比直接使用User model [Django-doc] 更好。更多信息可以查看referencing the User model section of the documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 2017-06-29
      • 2019-05-27
      • 1970-01-01
      • 2021-09-26
      相关资源
      最近更新 更多