studentprog
studentprog

Reputation: 25

Django QuerySet filtering not working with views, showing blank view even though there are entries

I am attempting to view a particular set of objects with a certain attribute by using QuerySet filtering, however when I use the filter, the view returns blank. Not sure if I'm using this particular filter wrong, or if I'm calling on the attribute wrong, however I accessed other attributes (as seen below, the attribute "status") and found that it worked fine.

views.py:

from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from .models import *
from .forms import *



@login_required
def vendorView(request):
    return render(request, 'inv/vendorInventory.html')

@login_required
def consumerView(request):
    return render(request, 'inv/consumerInventory.html')

def unauthenticatedView(request):
    return render(request, 'inv/unauthenticatedInventory.html')
################ need to edit other views to render consumer/vendor/unauth

def display_drinks(request):
    items = Drinks.objects.all()
    context = {
        'items': items,
        'header': 'Drinks',
    }

    if not request.user.is_authenticated:
        items = Drinks.objects.filter(status='AVAILABLE')
        context = {
            'items': items,
            'header': 'Drinks',
        }
        return render(request, 'inv/unauthenticatedInventory.html', context)

    elif request.user.profile.vendor:
        items = Drinks.objects.filter(donatorID=request.user.username)
        context = {
            'items': items,
            'header': 'Drinks',
        }
        return render(request, 'inv/vendorInventory.html', context)

    elif not request.user.profile.vendor:
        items = Drinks.objects.filter(status='AVAILABLE')
        context = {
            'items': items,
            'header': 'Drinks',
        }
        return render(request, 'inv/consumerInventory.html', context)

inventory/models.py:

from django.db import models
from django.contrib.auth.models import User
from users.models import *



# Create your models here.

class Donation(models.Model):

    description = models.CharField(max_length=200, blank=False, help_text='Describe your donation here')

    choices = ( #for status
        ('AVAILABLE', 'Item ready to be picked up'),
        ('RESERVED', 'Item reserved'),
    )

    expiry = models.CharField(max_length=200, blank=False, help_text="Enter expiration date here")
    status = models.CharField(max_length=10, choices=choices, default='AVAILABLE')
    misc = models.CharField(max_length=50, blank=False, help_text='Miscellaneous info about your donation')
    donatorID = models.CharField(max_length=50, default = User.username)



    class Meta:
        abstract = True

    def __str__(self):
        return '{0}'.format(self.description)


class Foods(Donation):
    pass

class Drinks(Donation):
    pass

class MiscObjects(Donation):
    pass

As one can see, in models.py, the donatorID is assigned to the Donation object, and is the user's username. In the function display_drinks (in views.py), in the first elif, it should use the user's username to filter whatever items don't have the matching attribute, place items into a QuerySet that do match - however it displays blank, even though there are items that have matching attributes.

Would anyone know why this is occurring?

Thanks.

edit: as requested, here's vendorInventory.html:

{% extends "blog/base.html" %}

{% block body %}



<center>  <div class="add_buttons">


    <div class="btn-group-vertical">
      <a href="{% url 'display_foods' %}" class="btn btn-outline-info" role="button"> View Food</a>
      <a href="{% url 'add_food' %}" class="btn btn-outline-info" role="button"> Add Food</a>
    </div>

    <div class="btn-group-vertical">
      <a href="{% url 'display_drinks' %}" class="btn btn-outline-info" role="button">View Drinks</a>
      <a href="{% url 'add_drink' %}" class="btn btn-outline-info" role="button"> Add Drink</a>
    </div>

    <div class="btn-group-vertical">
      <a href="{% url 'display_miscObjects' %}" class="btn btn-outline-info" role="button"> View Misc</a>
      <a href="{% url 'add_miscObject' %}" class="btn btn-outline-info" role="button"> Add Misc</a>
    </div>


  </div>

  </center>

  <div>

    <h4>Currently Viewing {{ header }}</h4>
  </div>

  <table class="table table-hover">
    <thead>
      <tr>
        <th>id</th>
        <th>Description</th>
        <th>Expiry Date</th>
        <th>Status</th>
        <th>Misc</th>
        <th>Edit/Delete</th>

      </tr>
    </thead>

    <tbody>

      {% for item in items %}

      <tr>
        <td>{{ item.pk }}
        <td>{{ item.description }}</td>
        <td>{{ item.expiry }} </td>
        <td>{{ item.status }}</td>
        <td>{{ item.misc }}</td>

        {% if header|lower == "drinks" %}
        <td>
            <a href="{% url 'edit_drink' item.pk %}" class="btn btn-outline-info" role="button" aria-pressed="true" > Edit</a>
            <a href="{% url 'delete_drink' item.pk%}" class="btn btn-danger btn-sm" role="button" aria-pressed="true" > x </a>
        </td>
        {% elif header|lower == "foods" %}
        <td>
            <a href="{% url 'edit_food' item.pk %}" class="btn btn-outline-info" role="button" aria-pressed="true" > Edit</a>
            <a href="{% url 'delete_food' item.pk%}" class="btn btn-danger btn-sm" role="button" aria-pressed="true" > x </a>
        </td>
        {% else %}
        <td>
            <a href="{% url 'edit_miscObject' item.pk %}" class="btn btn-outline-info" role="button" aria-pressed="true" > Edit</a>
            <a href="{% url 'delete_miscObject' item.pk%}" class="btn btn-danger btn-sm" role="button" aria-pressed="true" > x </a>
        </td>
        {% endif %}
      </tr>

      {% endfor %}

    </tbody>
  </table>

{% endblock %}

forms.py:

from django import forms
from .models import *


class DrinkForm(forms.ModelForm):
    class Meta:
        model = Drinks
        fields = ('description', 'expiry', 'status', 'misc', 'donator')


class FoodForm(forms.ModelForm):
    class Meta:
        model = Foods
        fields = ('description', 'expiry', 'status', 'misc')


class MiscObjectForm(forms.ModelForm):
    class Meta:
        model = MiscObjects
        fields = ('description', 'expiry', 'status', 'misc')

class ReserveDrinkForm(forms.ModelForm):
    class Meta:
        model = Drinks
        fields = ('status',)


class ReserveFoodForm(forms.ModelForm):
    class Meta:
        model = Foods
        fields = ('status',)


class ReserveMiscObjectForm(forms.ModelForm):
    class Meta:
        model = MiscObjects
        fields = ('status',)

Upvotes: 0

Views: 476

Answers (2)

Alexandr S.
Alexandr S.

Reputation: 1776

# models.py
from django.db import models
from users.models import *
# or if User model is not overwritten: from django.contrib.auth.models import User


class Donation(models.Model):
    choices = (  # for status
        ('AVAILABLE', 'Item ready to be picked up'),
        ('RESERVED', 'Item reserved'),
    )

    description = models.CharField(max_length=200, help_text='Describe your donation here')
    expiry = models.CharField(max_length=200, help_text="Enter expiration date here")
    status = models.CharField(max_length=10, choices=choices, default='AVAILABLE')
    misc = models.CharField(max_length=50, help_text='Miscellaneous info about your donation')
    donator = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        abstract = True

    def __str__(self):
        return self.description


class Foods(Donation):
    pass

class Drinks(Donation):
    pass

class MiscObjects(Donation):
    pass


# views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from users.models import *
# or if User model is not overwritten: from django.contrib.auth.models import User
from .models import *
from .forms import *



@login_required
def vendorView(request):
    return render(request, 'inv/vendorInventory.html')

@login_required
def consumerView(request):
    return render(request, 'inv/consumerInventory.html')

def unauthenticatedView(request):
    return render(request, 'inv/unauthenticatedInventory.html')
################ need to edit other views to render consumer/vendor/unauth

def display_drinks(request):
    items = Drinks.objects.all()
    context = {
        'items': items,
        'header': 'Drinks',
    }

    if not request.user.is_authenticated:
        items = Drinks.objects.filter(status='AVAILABLE')
        context = {
            'items': items,
            'header': 'Drinks',
        }
        return render(request, 'inv/unauthenticatedInventory.html', context)

    elif request.user.profile.vendor:
        items = Drinks.objects.filter(donator__username=request.user.username)
        context = {
            'items': items,
            'header': 'Drinks',
        }
        return render(request, 'inv/vendorInventory.html', context)

    elif not request.user.profile.vendor:
        items = Drinks.objects.filter(status='AVAILABLE')
        context = {
            'items': items,
            'header': 'Drinks',
        }
        return render(request, 'inv/consumerInventory.html', context)

This will work, BUT better to use class-based views here for example: official documentation: https://docs.djangoproject.com/en/dev/topics/class-based-views/

from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import TemplateView
from django.views.generic.list import ListView

# Better naming for model will be a "Drink" instead of "Drinks" (Django code style)
from .models import Drinks


class VendorView(LoginRequiredMixin, TemplateView):
    # better to use 'inv/vendor_inventory.html' naming style for PEP8 compatibility.
    template_name = 'inv/vendorInventory.html'


class ConsumerView(LoginRequiredMixin, TemplateView):
    template_name = 'inv/consumerInventory.html'

# Name for view, I think looks terrible ;)
class UnauthenticatedView(TemplateView):
    template_name = 'inv/unauthenticatedInventory.html'


class DrinksListView(ListView):
    model = Drinks
    context_object_name = 'items'    # Variable in template, better to use something like: context_object_name = 'drinks'

    def get_queryset(self):
        if self.request.user.is_authenticated():
            return Drinks.objects.filter(status='AVAILABLE')
        elif self.request.user.profile.vendor:
            return Drinks.objects.filter(donator__username=self.request.user.username)
        # Better to use "else:" here instead of "elif" (for all other logic).
        elif not self.request.user.profile.vendor:
            return Drinks.objects.filter(status='AVAILABLE')

Answers for questions from comments:

If you want to add automatically user for every new Drinks object, you should do something like this: First of all, you should exclude field "donator" in your form:

# forms.py
class MyForm(forms.ModelForm):
    #.....
    class Meta: 
        model = Drinks
        exclude = ('donator', )

If you use function-based views: you should add something like this:

# views.py
if request.GET: 
    form = MyForm()
if request.POST:
    form = MyForm(request.POST)
    if form.is_valid():
        drinks = form.save(commit=False)
        drinks.donator = request.user
        drinks.save()
return render(request, 'my_temlate.html', {'form': form}) 

If you use class-based views: you should overwrite "post" method same way. You can find out more here, also there are examples: How do I use CreateView with a ModelForm

Upvotes: 1

replace this in model donatorID = models.ForeignKey(User, on_delete=models.CASCADE)

While saveing the data pass Users Object : variable_name = User.objects.get(username=request.user)

Upvotes: 1

Related Questions