Search code examples
pythondjangodjango-modelsforeign-keysdjango-database

Django: How to only display content made by a user to that same user?


I'm learning Django (obviously) and one thing I just noticed about a project I'm building is that initially, Each user could see objects/content made by every other user. For instance, my project is a very basic fantasy football app, I've set it up so that a user can create a team by adding listed players to my team model called "MyTeam" as seen here.

class Myteam(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    QB = models.CharField(max_length=80, null=True)
    QBscore = models.IntegerField(null=True)
    RB1 = models.CharField(max_length=80, null=True)
    RB1score = models.IntegerField(null=True)
    RB2 = models.CharField(max_length=80, null=True)
    RB2score = models.IntegerField(null=True)
    WR = models.CharField(max_length=80, null=True)
    WR1score = models.IntegerField(null=True)
    WR2 = models.CharField(max_length=80, null=True)
    WR2score = models.IntegerField(null=True)
    TE = models.CharField(max_length=80, null=True)
    TEscore = models.IntegerField(null=True)
    D = models.CharField(max_length=80, null=True)
    Dscore = models.IntegerField(null=True)
    K = models.CharField(max_length=80, null=True)
    Kscore = models.IntegerField(null=True)

I've learned about ForeignKeys for linking specific content to a particular user and the next step I'm trying to figure out is how to get it so that a logged-in user can only see their team and no one else's. I've been searching around here and other places via google, but as a Django noob, it's hard to even verbalize the right question to search for in the first place.

I thought I was on the right track when I saw somewhere that I might need to filter the content being returned in my views.py. Here's something I tried earlier that didn't work.

def index(request):
    
    team = Myteam.objects.filter(user=request.user.username)

This always returns an error of...

Cannot resolve keyword 'user' into field. Choices are: D, Dscore, K, Kscore, QB, QBscore, RB1, RB1score, RB2, RB2score, TE, TEscore, WR, WR1score, WR2, WR2score, author, author_id, id

previously, my code in views.py > def index was

def index(request):
    
    team = Myteam.objects.get()

This was obviously getting all objects in the database. I understand I need to filter out by user, but I'm just not quite sure how to get that working. I appreciate your time and attention if you look at this.

This is my user class.

from django.contrib.auth.models import AbstractUser
from django.db import models
import datetime



class User(AbstractUser):
    pass

Here is login.html

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

{% block body %}
<div class="container">
<div class="card">
    <div class="card-body">
        <h2>Login</h2>

        {% if message %}
            <div>{{ message }}</div>
        {% endif %}

        <form action="{% url 'login' %}" method="post">
            {% csrf_token %}
            <div class="form-group">
                <input autofocus class="form-control" type="text" name="username" placeholder="Username">
            </div>
            <div class="form-group">
                <input class="form-control" type="password" name="password" placeholder="Password">
            </div>
            <input class="btn btn-primary btn-block" type="submit" value="Login">
        </form>
        <br>
        Don't have an account? <a href="{% url 'register' %}">Register here.</a>
    </div>
</div>
</div>

{% endblock %}

Solution

  • Write your view this way

    def index(request):
    
        team = Myteam.objects.filter(author=request.user).first()
    

    or

    def index(request):
    
        team = Myteam.objects.filter(author_id=request.user.id).first()