Forums

Filter query does not work

Hello guys. So I am developing using django and i happened to create a Record model with the writer as the user foreign key but when I filter I still get all the model instances diplayed. The code is as follows

Models.py

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Record(models.Model):
    writer = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    record_entry = models.CharField(max_length=100)
    date = models.CharField(max_length=50)
    time = models.TimeField(max_length=20)
    snap = models.ImageField()
    mood = models.CharField(max_length=200)
    general_Mood = models.CharField(choices=MOOD, max_length=50)

    def __str__(self):
        return self.record_entry


class Entry(models.Model):
    title = models.ForeignKey(Record, on_delete=models.CASCADE)
    event = models.CharField(max_length=60)
    event_Date = models.DateField(max_length=60)
    time = models.TimeField(max_length=20)
    happening = models.TextField()

    def __str__(self):
        return self.event

Views.py

from django.shortcuts import render, get_object_or_404, redirect
from .models import Record, New, Entry
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login, authenticate
from .forms import RecordForm, EntryForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
# Create your views here.


def index(request):
    return render(request, 'records/index.html')


def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('records:index')
    else:
        form = UserCreationForm()
    return render(request, 'records/signup.html', {'form': form})


@login_required
def details(request):
    records = Record.objects.filter(writer=request.user)
    news = New.objects.all()
    return render(request, 'records/details.html', {'records': records, 'news': news})


@login_required
def record_details(request, record_id):
    record = get_object_or_404(Record, pk=record_id)
    return render(request, 'records/record_details.html', {'record': record})


@login_required
def create_record(request):
    form = RecordForm(request.POST, request.FILES)
    if form.is_valid():
        record = form.save(commit=False)
        record.snap = request.FILES['snap']
        record.writer = request.user
        record.save()
        return render(request, 'records/record_details.html', {'record': record})
    return render(request, 'records/create_record.html', {'form': form})


@login_required
def delete_record(request, record_id):
    record = get_object_or_404(Record, pk=record_id)
    record.delete()
    return redirect('records:index')

details.html template

{% for record in records %}
<div class="col-sm-3">
  <div class="panel panel-success" style="height: 500px; overflow-y: scroll;">
     <div class="panel panel-body">
        <a href="{% url 'records:record_details' record.id %}">
         <img src="{{ record.snap.url }}" class="img img-rounded img-responsive" alt="Image Coming Soon" style="height: 300px;max-width: 250px;">
         </a>
         <br>
        <a href="" class="btn btn-info btn-sm"><i class="fas fa-edit"></i></a>
        <a href="" class="btn btn-danger btn-sm"><i class="fas fa-heart"></i></a>
        <a href="" class="btn btn-warning btn-sm"><i class="fas fa-trash"></i></a>
        <h3>{{ record.record_entry }}</h3>
        <h4>{{ record.date }} {{ record.time }}</h4>
        <h5>General Mood: {{ record.general_Mood }}</h5>
            </div>
          </div>
   </div>
{% endfor %}

Help me out. Been moving around in circle. By the way, in localhost it works but it doesn't when I push it to the site.

maybe check your database to see what exactly are the records and users in your database?

I wonder if there has been any solution to this, I'm having this exact same problem right now

Check your database entries and make sure that the filter that you are applying does not include them all.

Thanks for your help Glenn, I finally got it working now

Glad to hear that!