在Django中遇到查询问题

时间:2017-12-23 17:14:18

标签: django django-models django-views

我正在Django中构建我的第一个应用程序,而我正在尝试查询"加载"基于我已加载到负载的公司。这是有问题的模型。

class Load(models.Model):
    company = models.ForeignKey(UserCompany, null=True, 
    on_delete=models.CASCADE)
    load_number = models.IntegerField()
    carrier = models.CharField(max_length=255)
    pickup_date = models.DateField()
    delivery_date = models.DateField()
    shipper = models.CharField(max_length=255)
    consignee = models.CharField(max_length=255)
    po_number = models.CharField(max_length=255)
    pu_number = models.CharField(max_length=255)
    pieces = models.IntegerField()
    description = models.TextField()
    date_created = models.DateTimeField(blank=True, null=True)

    def publish(self):
        self.date_created = timezone.now()
        self.save()

    def __str__(self):
        return str(self.load_number)

现在我试图在页面上显示列表,但只显示附加到特定公司的负载。用户也需要连接到该公司,所以这是我的用户模型。

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

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

# Create your models here.

class UserCompany(models.Model):
    company_name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.company_name

    def __str__(self):
        return self.company_name

# User Model
class UserProfileInfo(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    # Additional Classes
    profile_pic = models.ImageField(upload_to='profile_pics', 
    blank=True)
    company = models.ForeignKey(UserCompany, 
    null=True,on_delete=models.CASCADE)

    def __str__(self):
        return self.user.username

然后我试图查询"加载"在这个观点内。

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render, get_object_or_404
from django.shortcuts import redirect
import datetime
from django.conf import settings
from django.utils import timezone
from django.http import HttpResponse
from django.views.generic import View, DetailView
from easy_pdf.views import PDFTemplateResponseMixin
from loads.models import Load
from .forms import LoadForm
from users.models import UserCompany, UserProfileInfo

# Create your views here.

class PDFUserDetailView(PDFTemplateResponseMixin, DetailView):
    model = Load
    template_name = 'loads/load_pdf.html'

def load_list(request):
    loads = 
Load.objects.filter(company=request.company).order_by('date_created')
    return render(request, 'loads/load_list.html', {'loads':loads})

我能够根据用户获得查询工作,所以我的想法是这个查询是一样的。不是这种情况。我有一种感觉,我引用了公司的错误,也许我需要在原来的UserCompany类中再过滤2个级别,只是不确定如何实现。

我得到的错误是:

AttributeError: 'WSGIRequest' object has no attribute 'company'

1 个答案:

答案 0 :(得分:2)

imageView.setImageBitmap(decodeSampledBitmap(filePath, imageView.getWidth(), imageView.getMaxHeight())); 不是你能做的事情。而不是

request.company

你尝试这样的事情:

Load.objects.filter(company=request.company).order_by('date_created')

current_user = request.user company = current_user.userprofileinfo.company Load.objects.filter(company=company).order_by('date_created') 只保存有关当前请求的信息,您可以阅读有关here的更多信息