0

I am trying to route an AJAX GET request through views.py and return it to a template, but only show the object items connected with the logged in user.

I am attempting to use filter() but am getting the following error: 'User' object is not iterable.

Here is the views.py class:

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)

Models:

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

Here is the template:

<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 %}

And here is the AJAX Call for good measure:

//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);
                    }
    });
    });

For those interested here is the full traceback:

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
5
  • can you post your model too ? Commented Oct 8, 2020 at 10:52
  • Hey thanks! Added to OP Commented Oct 8, 2020 at 10:54
  • it seems like your UserVenue model dosen't have a relation with the django UserModel? Commented Oct 8, 2020 at 10:56
  • The Django User model being the one from django.contrib.auth.models (which is what I've used)? How would I go about relating the two? Commented Oct 8, 2020 at 11:00
  • can you post your UserList model too ? Commented Oct 8, 2020 at 11:00

1 Answer 1

1

Use this query

 return UserVenue.objects.filter(list__user=self.request.user)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that has worked! However it is not showing on the template only in the Chrome console Preview tab in network. Do you know why this may be?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.