Jack Clarkson
Jack Clarkson

Reputation: 105

django Field 'id' expected a number but got <built-in function id>

I'm learning this tutorial about django todoapp. I want to display all user's content (their todolists) and well im getting that error see the title I'm using google's login auth

here's my views.py

def todoView(request,):
  all_todo_items = Todoitem.objects.filter(userid=id).values_list('id', flat=True)
  return render(request, 'todoapp.html', {'all_items': all_todo_items})

here's my models.py

class Todoitem(models.Model):
content = models.CharField(max_length=100)
userid = models.ForeignKey(AuthUser, models.DO_NOTHING, db_column='userid', blank=True, 
null=True)

class Meta:
    managed = False
    db_table = 'todoitem'

def __str__(self):
    return self.content

class AuthUser(models.Model):
password = models.CharField(max_length=128)
username = models.CharField(unique=True, max_length=150)
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
email = models.CharField(max_length=254)

class Meta:
    managed = False
    db_table = 'auth_user'

here's my todoapp.html

{% load socialaccount %}
<body>
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }} !</p>

{% else %}
<h1>My Google Login Project</h1>
<a href="{% provider_login_url 'google' %}">Login with Google</a>
{% endif %}
<h2>My Personal TodoApp Project</h2>
<br>
<table>
<tr>
    <th colspan="2">List of Todos</th>
</tr>
{% for todo_items in all_items %}
<tr>
    <td>{{todo_items.content}}</td>
</tr>
{% endfor %}
</table>
</body>

Upvotes: 1

Views: 568

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477854

You pass the Todoitems of the logged in user with:

from django.contrib.auth.decorators import login_required

@login_required
def todoView(request):
    all_todo_items = Todoitem.objects.filter(userid=request.user)
    return render(request, 'todoapp.html', {'all_items': all_todo_items})

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.


Note: You can limit views to a view to authenticated users with the @login_required decorator [Django-doc].

Upvotes: 1

Biplove Lamichhane
Biplove Lamichhane

Reputation: 4095

I expect id comes from the path. So, add id parameter in your function:

def todoView(request, id=None):
  all_todo_items = Todoitem.objects.filter(userid=id).values_list('id', flat=True)
  return render(request, 'todoapp.html', {'all_items': all_todo_items})

Upvotes: 2

Related Questions