Django QuerySet过滤不适用于视图,即使有条目也显示空白视图

时间:2019-01-03 11:32:31

标签: django django-models django-views

我试图通过使用QuerySet过滤来查看具有特定属性的一组特定对象,但是当我使用过滤器时,该视图将返回空白。不知道我使用的是特定的过滤器是否错误,还是调用的属性错误,但是我访问了其他属性(如下所示,属性“状态”)并发现它工作正常。

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

可以看到,在models.py中,donatorID被分配给Donation对象,并且是用户的用户名。在函数display_drinks中(在views.py中),在第一个省略号中,它应使用用户名来过滤不具有匹配属性的任何项目,将项目放入确实匹配的QuerySet中-但是,即使显示为空白,有些项目具有匹配的属性。

有人知道为什么会这样吗?

谢谢。

编辑:根据要求,这里是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',)

2 个答案:

答案 0 :(得分:1)

将此替换为模型 donatorID = models.ForeignKey(User, on_delete=models.CASCADE)

保存数据时,通过用户对象:variable_name = User.objects.get(username=request.user)

答案 1 :(得分:1)

# 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)

这将起作用,但是最好在此处使用基于类的视图: 官方文档:documentation

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')

回答评论的问题:

如果要为每个新的Drinks对象自动添加用户,则应执行以下操作: 首先,您应在表单中排除字段“捐赠者”:

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

如果您使用基于功能的视图:  您应该添加如下内容:

# 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}) 

如果使用基于类的视图,则应以相同的方式覆盖“ post”方法。您可以在此处找到更多信息,也有示例:https://docs.djangoproject.com/en/dev/topics/class-based-views/