Wasim Akhtar Khan
Wasim Akhtar Khan

Reputation: 63

Field 'id' expected a number but got 'xyz'

In Models.py

class Post(models.Model):
    user = models.CharField(max_length=100)
    likes = models.IntegerField(default=0)
    content = models.TextField()
    date = models.DateTimeField(auto_now_add=True)

class Profile(models.Model):
    following = models.ForeignKey('User',on_delete=models.CASCADE,related_name='following')
    user = models.ForeignKey('User',on_delete=models.CASCADE,related_name='user')
    
    def __str__(self):
        return self.user

In views.py

def viewProfile(request,username):
    posts = Post.objects.filter(user=username).order_by('id').reverse()
    profile = Profile.objects.filter(user=username)
    no_of_followers = profile.following.count()
    return render(request, "network/profile.html",{
        "posts":posts,
        "username":username,
        "no_of_followers":no_of_followers
    })

In profile.html

{% extends "network/layout.html" %}

{% block body %}
    <h2 style="margin-left: 20px;">Posts of {{username}}</h2>
    <div class="col-sm-6">
        <div class="card">
          <div class="card-body">
            <h5 class="card-title">Profile Details</h5>
            <p class="card-text">Followers:{{no_of_followers}}</p>
            <p class="card-text">Followings:0</p>
            
            <a href="#" class="btn btn-primary">Go somewhere</a>
            
        </div>
        </div>
      </div>
    {% for post in posts %}
        <div class="card" style="width:70%;margin-left: 10%;margin-right: 20%;">
            <div class="card-body">
                <a href="{% url 'viewprofile' post.user %}"><h5 class="card-title">{{post.user}}</h5></a>
                <div class="form-group">
                    <p>{{post.date}}<br>{{post.content}}</p>
                </div>
                <p>{{post.likes}}</p>
            </div>
        </div>
    {% endfor %}
{% endblock %}

Facing an error of Field 'id' expected a number but got 'xyz'. xyz is the username

If I replace profile = Profile.objects.filter(user=username)with profile = Profile.objects.filter(user__user=username) then I am getting the error django.core.exceptions.FieldError: Related Field got invalid lookup: user

Upvotes: 2

Views: 315

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476699

The lookup should be:

profile = Profile.objects.get(user__username=username)

since you want to retrieve a Profile for which the user has a username that has the given username.

You probably should use .get(…) [Django-doc] here to retrieve a Profile object, not a QuerySet of Profiles.

You might want to use the get_object_or_404(…) function [Django-doc] instead, such that it returns a 404 in case no such profile exists:

from django.shortcuts import get_object_or_404

def viewProfile(request,username):
    posts = Post.objects.filter(user__username=username).order_by('-id')
    profile = get_object_or_404(Profile, user__username=username)
    no_of_followers = profile.following.count()
    return render(request, 'network/profile.html',{
        'posts':posts,
        'username':username,
        'no_of_followers":no_of_followers
    })

Upvotes: 1

Related Questions