TypeError:“用户”对象不能使用Django

2024-10-02 20:30:42 发布

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

我试图通过views.py路由AJAX GET请求并将其返回到模板,但只显示与登录用户连接的对象项

我正试图使用filter(),但出现以下错误:'User' object is not iterable

下面是views.py类:

class user_playlist(ListView):
    template_name = 'testingland/dashboard.html'
    context_object_name = 'playlist'
    model = UserVenue

    def get_queryset(self):
        venue = self.request.GET.get('venue', None)
        list = self.request.GET.get('list', None)

        return UserVenue.objects.filter(self.request.user)

型号:

class UserVenue(models.Model):
    venue = models.ForeignKey(mapCafes, on_delete=models.PROTECT)
    list = models.ForeignKey(UserList, on_delete=models.PROTECT)

class UserList(models.Model):
    list_name = models.CharField(max_length=255)
    user = models.ForeignKey(User, on_delete=models.CASCADE) 

    def __str__(self):
        return self.list_name

以下是模板:

<body>
    {% block content %}
        <div class="container-fluid" style="padding:15px">
            <!--location -->
            <div class="row">
              <div class="col-sm-3" id="playlist"></div>
                <ul class = "cafe-playlist">
                  {% for item in playlist %}
                    <li>
                      <div>
                          <h6 class="playlist-name">{{ item.list }}</h6>
                      </div>
                  </li>
                  {% endfor %}
                </ul>
              </div>
    {% endblock %}

下面是AJAX对良好措施的呼吁:

//dashboard
$(document).ready(function(){
    console.log('Document Ready')
    $.ajax({
            type: "GET",
             url : '/electra/playlist',
             dataType: "json",
             data: {
                 'venue': 'venue',
                 'list':  'list',
                },
                    success: function(data){
                    $("#playlist").html(data);
                    console.log(data)
                    },
                    failure: function(errMsg) {
                        alert(errMsg);
                    }
    });
    });

对于那些感兴趣的人,这里是完整的回溯:

Internal Server Error: /electra/playlist
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/avenv/lib/python3.6/site-packages/django/views/generic/base.py", line 73, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/views/generic/base.py", line 101, in disp
atch
return handler(request, *args, **kwargs)
  File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/views/generic/list.py", line 142, in get
    self.object_list = self.get_queryset()
  File "/Users//Desktop/Coding/anybody/anybody1/testingland/views.py", line 59, in get_queryset
    return UserVenue.objects.filter(self.request.user)
  File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manage
r_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 ad
d_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 _a
dd_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 1255, in bu
ild_filter
    arg, value = filter_expr
  File "/Users//Desktop/Coding/anybody/avenv/lib/python3.6/site-packages/django/utils/functional.py", line 241, in inner
    return func(self._wrapped, *args)
TypeError: 'User' object is not iterable

Tags: djangoinpyselfmodelslibpackagesline